This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes collection instantiation question -- was Guide me please Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "collection instantiation question -- was Guide me please" Watch "collection instantiation question -- was Guide me please" New topic
Author

collection instantiation question -- was Guide me please

Carlos G�mez
Ranch Hand

Joined: Sep 06, 2006
Posts: 56
What is the diference between

List ls3 = new ArrayList();
and
List ls2 = new ArrayList<String>();




[EDIT: slight modification to subject title]
[ September 08, 2006: Message edited by: Henry Wong ]
Surendra Kumar
Ranch Hand

Joined: Jul 04, 2006
Posts: 235
No difference as to adding objects to this list (Java 1.5).
Carlos G�mez
Ranch Hand

Joined: Sep 06, 2006
Posts: 56
Thank you Surendra !!!
Pranav Bhatt
Ranch Hand

Joined: Mar 20, 2006
Posts: 284
Carlos,
Before Java 5.0 we don't had the option of defining the TYPE of array(i.e the type of objects the array will hold). So writing-:
List ls3 = new ArrayList();
implies a list of array that can hold all sort of objects(strings,integer n all).You had to wrap primitives into the object type and then unwrap primitives to access them.
But writing-:
List ls2 = new ArrayList<String>();
implies that now we can have a particular type of object array,string in your case. This has been introduced in Java 5.0. You can straightway convert primitives into objects for arrays and compiler automatically unwraps them later.
This is what Autoboxing is
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Actually when you say:



You're instantiating an ArrayList which is declared to hold type String, and assigning it to a reference of a raw List type. Doing it this way you still have to cast each Object retrieved from the List. However if you declare:
no casting is necessary, you get a compile time "guarantee" that the List reference will only hold String references.

BTW: This has nothing to do with autoboxing. It is a feature of generics.


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Pranav Bhatt
Ranch Hand

Joined: Mar 20, 2006
Posts: 284
Hey you got me wrong, Autoboxing was for the finishing lines of my explanation the wrapping and unwrapping part.That is autoboxing does the conversion from primitive to wrapper object automatically.. i fell i have made myself clear now
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Carlos Gomez asked
What is the difference between

List ls3 = new ArrayList();
and
List ls2 = new ArrayList<String>();


The first line uses legacy code only. ls3 will take only objects.
ls2 will also take plain objects, the difference is only that here legacy code and generics are mixed, which is legal and won't produce a warning when it is done like in this example.
But it should be done like Garett posted (see above).
Coding like in the lines with ls3 and ls2 should be done only if you use older classes of the pre-generic age.

BTW, the following would produce a warning:

In a way the opposite of your second line.
Because in ls3 there may be other types than String it may cause a class cast exception when trying to get a String out of listS.



And yes, it does not have anything to do with autoboxing.

Yours
Bu.


all events occur in real time
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Originally posted by pranav bhatt:
Hey you got me wrong, Autoboxing was for the finishing lines of my explanation the wrapping and unwrapping part.That is autoboxing does the conversion from primitive to wrapper object automatically.. i fell i have made myself clear now


You're right, I misread the original post. Sorry about that.
Carlos G�mez
Ranch Hand

Joined: Sep 06, 2006
Posts: 56
Thank you all, your post are helpful for me.

I tried to decompile the java file with jad and these are the results:

Before erasure .java
---------------------------------------
List listRaw = new ArrayList<String>();
List listRaw2 = new ArrayList();
---------------------------------------

After erasure (after to decompile the java file)
---------------------------------------
ArrayList arraylist = new ArrayList();
ArrayList arraylist1 = new ArrayList();
---------------------------------------

You are right !!!

[ September 09, 2006: Message edited by: Carlos Gomez ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: collection instantiation question -- was Guide me please