aspose file tools*
The moose likes Java in General and the fly likes Difference between List<String> list1 = new ArrayList(); and List<String> list2 = new ArrayList<Stri Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Difference between List<String> list1 = new ArrayList(); and List<String> list2 = new ArrayList<Stri" Watch "Difference between List<String> list1 = new ArrayList(); and List<String> list2 = new ArrayList<Stri" New topic
Author

Difference between List<String> list1 = new ArrayList(); and List<String> list2 = new ArrayList<Stri

Naresh Shanmugam
Ranch Hand

Joined: Jul 16, 2010
Posts: 84
(1) List<String> list1 = new ArrayList();
(2) List<String> list2 = new ArrayList<String>();

With both of these options it allows us to add only String into the list.
Is there any other difference between those two??

Is it okey if i just use List<String> list1 = new ArrayList();

What is real significance of List<String> list2 = new ArrayList<String>();
than List<String> list1 = new ArrayList();
Garik Ustinov
Ranch Hand

Joined: Jun 22, 2009
Posts: 31
Yes, it is allowed to do it this way in order to achieve backward compatibility with previous Java versions.
And no, you don't want to code like this.
Consider the following:


The compiler would be happy to compile this, but guess what you get in List<String>? An object!
By declaring a Collection<String>, you want to be sure that you get only Strings in there, while a raw type collections allow you to add anything. One day after refactoring your new ArrayList() will be returned from a separate method. Maybe the next day someone will add something to it and who knows, maybe this something won't be a String...


SCJP, SCWCD
Naresh Shanmugam
Ranch Hand

Joined: Jul 16, 2010
Posts: 84
With List<String> list1 = new ArrayList();

(1) We cant add anything other than "String"
(2) List<Object> list2 = list1 will lead to COMPILER error


Similarly with List<String> list2 = new ArrayList<String>();

(1) We cant add anything other than "String"
(2) List<Object> list2 = list2 will lead to COMPILER error

Then what is the difference between those two..
List<String> list1 = new ArrayList(); and List<String> list2 = new ArrayList<String>();


Can any one give explanation please
I am confused !!!

Garik please let your view about this...
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Honestly, there is no real difference, but the only reason the second version is allowed is to make it easier to compile code that uses generics with old code that does not. It's just better style to include the <String> in both places.


[Jess in Action][AskingGoodQuestions]
Ram Chhabra
Greenhorn

Joined: Jun 23, 2007
Posts: 23
Sorry, but what i understood from the discussion is that, the code:
will work fine in JDK 1.4... ???
Manjusha Muraleedas
Ranch Hand

Joined: Jun 02, 2010
Posts: 52
As of my knowledge, will throw a compile time "Warning".

if you use the following ... it wont give the warning...

in boh cases compiler restrinct addition of objects other than those of 'String ' wll give a compilation error.

(I don't know whether any other tricky things hided behind this.mixing of generics to legecy code has always been tricky for me)
Naresh Shanmugam
Ranch Hand

Joined: Jul 16, 2010
Posts: 84
Thanks Ernest.. I got the point now..
 
Consider Paul's rocket mass heater.
 
subject: Difference between List<String> list1 = new ArrayList(); and List<String> list2 = new ArrayList<Stri