| Author |
Instantiate a generic type
|
Sasha Ruehmkorf
Ranch Hand
Joined: Mar 29, 2007
Posts: 115
|
|
Hi. Is there any difference between these two declarations apart from the cryptic compiler warning, I get with first line? Is there anything that will compile with list1 but not with list2 or any following code that will produce a warning with list1 but would not with list2? Thanks in advance. [ May 22, 2007: Message edited by: Sasha Ruehmkorf ]
|
 |
krishna bulusu
Ranch Hand
Joined: Aug 28, 2006
Posts: 185
|
|
There is lots of difference is there between the two lines. First one will be compiles with warning in JDK1.4 Second one will not be compiled in JDK1.5 In JDK1.5, You use generics to declare which kind of data a List can contain. Here you declare(Second one) as if the list can contain only Integer type of objects. this feature is not availabe in JDK1.4!!! First declaration may contain any type of object. Hope you gor the point. thanks®ards, krishna
|
Thanks&Regards, Krishna.
SCJP1.4, SCWCD1.4, SCBCD 5.0
|
 |
Anupam Sinha
Ranch Hand
Joined: Apr 13, 2003
Posts: 1088
|
|
|
Generices was introduced in 1.5 hence both the lines would not compile in JDK 1.4.
|
 |
Sasha Ruehmkorf
Ranch Hand
Joined: Mar 29, 2007
Posts: 115
|
|
Hope you gor the point.
Not really. I am aware that the above code won't compile using Java 1.4, and as I mentioned, I am also aware of the warning, the first line gives compiling with Java 5. But are you able to continue the above code in any way, so that list1 and list2 are not completely exchangeable? Is there anything I can get away with with list1 but not list2?
|
 |
krishna bulusu
Ranch Hand
Joined: Aug 28, 2006
Posts: 185
|
|
in list2, you are adding extra filter to your container ie, List. If it gets any thing other than Integer, it throws Exception. Its all your wish to declare a List based on your requirement.
|
 |
Chandra Bhatt
Ranch Hand
Joined: Feb 28, 2007
Posts: 1707
|
|
Hi, List list1 = new ArrayList<Integer>(); //No warning Note: Compiler is concerned about the ref type. If it is type safe, it will show warning is it is assigned non-type safe object. If ref type is non type safe and you assign it a type-safe object no matter, it will show warning when you add anything to it. Type safety of object does not matter here. Ref type should be type safe too. Thanks,
|
cmbhatt
|
 |
Sasha Ruehmkorf
Ranch Hand
Joined: Mar 29, 2007
Posts: 115
|
|
in list2, you are adding extra filter to your container ie, List. If it gets any thing other than Integer, it throws Exception.
it won't throw any Exception, it will give compiler error in this case, but that's exactly the same with list1, because of the same reference type.
Compiler is concerned about the ref type. If it is type safe, it will show warning is it is assigned non-type safe object.
yeah, I see that the compiler warns me. But to state my question in a different way: Should this warning be paid attention to and if so: why?
|
 |
krishna bulusu
Ranch Hand
Joined: Aug 28, 2006
Posts: 185
|
|
code: List<Integer> list2 = new ArrayList<Integer>(); list2.add("bala"); The above code will not compile!!! where is the question of warning here???
|
 |
Sasha Ruehmkorf
Ranch Hand
Joined: Mar 29, 2007
Posts: 115
|
|
list2.add("bala"); The above code will not compile!!!
I don't know how I could explain my question more clearly. Of course the above code won't compile, but I am absolutely aware of that fact and my point is:
list1.add("bala");
won't compile either! And I am just interested in any difference between list1 and list2 (as I doubt that there are any, but I'm not sure) [ May 22, 2007: Message edited by: Sasha Ruehmkorf ]
|
 |
krishna bulusu
Ranch Hand
Joined: Aug 28, 2006
Posts: 185
|
|
I am not getting your point man. Let me explain clearly. In JDK 1.5, This is the code: public class GenericsEx { /** Creates a new instance of GenericsEx */ public GenericsEx() { } public static void main(String[] args) { List list1 = new ArrayList(); list1.add("bala");//This will compile List<Integer> list2 = new ArrayList<Integer>(); list2.add("bala");// This won't compile } } I think i am making sence!!!
|
 |
Sasha Ruehmkorf
Ranch Hand
Joined: Mar 29, 2007
Posts: 115
|
|
Interesting, but it has got nothing to do with my question. My declaration for list1 was:
|
 |
Sasha Ruehmkorf
Ranch Hand
Joined: Mar 29, 2007
Posts: 115
|
|
|
No ideas?
|
 |
 |
|
|
subject: Instantiate a generic type
|
|
|