• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

collection instantiation question -- was Guide me please

 
Carlos G�mez
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 236
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No difference as to adding objects to this list (Java 1.5).
 
Carlos G�mez
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Surendra !!!
 
Pranav Bhatt
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Pranav Bhatt
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic