Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-05-2008, 07:48 PM
Member
 
Join Date: Aug 2008
Posts: 2
shah123khan is on a distinguished road
Generics for robustness ? Think again this example...Does it work?
Declaring an array with typed generics is illegal. E.g.

List<String> [] a = new ArrayList<String> [3];// is illegal

Why it is so? I have a reason: Assume that they are legal, what will be the complications? Consider the following.
---------------------------------1----------------------------------------------------------------------------------------
List<String> [] listArray = new ArrayList<String>[10]; // assume it is legal
Object[] oa = listArray;
oa[0] = new ArrayList<Integer>();
….
String a = listarray[0].get(0);//produces ClassCastException
--------------------------------2----------------------------------------------------------------------------------------
List<?>[] listArray = new ArrayList<?>[10];
Object[] oa = listarray;
oa[0] = new ArrayList<Integer>();
Integer a = (Integer) listArray[0].get(0);//explicit cast is required or else compile error

Having array declared as “new ArrayList<String>[10]” is not safe compared to “new ArrayList<?>[10]”. The reason is because the compiler can detect type error in the latter case than the former. The former results in runtime ClassCastException Exception. Generic is design mainly to prevent such type error.

Having understood the previous case, consider the following requirement in using generics.

“primitive types and array types cannot be used as type bounds. However array types can be used as wildcard bounds. “

For example:

void method1(A<T extends ArrayList<String>[]> a){} is illegal
void method2(A<? extends ArrayList<String>[]> b){} is legal

I don’t understand why using array types as wildcard bounds are OK but NOT for type bounds. I made an experiment and made a conclusion that array types cannot be used as both type bounds and wildcard bounds. Something is strange. Why did the SUN specification group allow it? I will reason by using the following example.
------------------------------------------

import java.util.*;
public class A<T>{
T[] y;

public void method(A<? extends AbstractList<String>[]> x){

/* No compilation error: reference variable y recognize itself as an array as it is declared as such and can take in multidimensional array because of "? extends AbstractList<String>[]" type */

x.y[0][0] = new ArrayList<String>();* //Line 1 *
Object[][] d = x.y;
ArrayList<Integer> g = new ArrayList<Integer>(); //an ArrayList of Int
g.add(4);
d[0][0] = g; //ok, no compilation error/warning
String f = x.y[0][0].get(0);// ok, no compilation error/warning
System.out.println(f);
}
public void add(T s){
/* Unchecked cast warning: the reason i think; for casting the left-hand and the right-hand sides must have subtype and supertype relationship. Here it is not certain, that's why the error. */
y = (T[])s; *// Line 2 *
}

public static void main(String[] argv){
A<ArrayList<String>[]> p = new A<ArrayList<String>[]>();
ArrayList<String>[] g = new ArrayList[2]; //Line 3: compilation warning
g[0] = new ArrayList<String>();
g[0].add("George");
g[1] = new ArrayList<String>();
g[1].add("Bush");
p.add(g);
A<ArrayList<String>[]> f = new A<ArrayList<String>[]>();
f.method(p);
}
}
-----------------------------------------------------------------
Except for 2 compilation warnings at line 2 and 3, there are no other errors/warnings. But at runtime the following exception is produced.

Exception in thread "main" java.lang.ClassCastException: [Ljava.util.ArrayList;
cannot be cast to [[Ljava.util.AbstractList;

Why it is so? Using generics should not produce ClassCastException. For this reason I feel array types cannot be used as type bounds and wildcard bounds.

Anyone willing to give your clarifications?

Thank you.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-06-2008, 07:07 AM
fishtoprecords's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 533
fishtoprecords is on a distinguished road
Why are you assuming code that won't compile is "// legal"?
The proper format is:

Code:
List<String> [] listArray = new ArrayList<String>(10);
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 08-06-2008, 05:10 PM
Member
 
Join Date: Aug 2008
Posts: 2
shah123khan is on a distinguished road
The assumption

List<String> [] listArray = new ArrayList<String>[10];

is legal is there to show what might happen if it is allowed. It will produce ClassCastException. But having this

List<?>[] listArray = new ArrayList<?>[10];

requires explicit cast and no runtime exception is produced. Those two examples serve as a background understanding for the actual consequences.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 08-06-2008, 11:06 PM
fishtoprecords's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 533
fishtoprecords is on a distinguished road
I simply can't follow you.
What are you asking for?
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
request.get not using generics Nicholas Jordan Java Servlet 4 07-27-2008 04:09 PM
LinkedList (Generics) JavaForums Java Blogs 0 06-28-2008 02:00 PM
Help w/ generics Hollywood New To Java 2 02-16-2008 05:08 AM
Generics sireesha New To Java 2 01-11-2008 01:08 AM
Generics eva New To Java 2 01-04-2008 11:10 PM


All times are GMT +3. The time now is 09:40 AM.


VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org