Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Naresh Shanmugam
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(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
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Naresh Shanmugam
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ram Chhabra
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, but what i understood from the discussion is that, the code:
will work fine in JDK 1.4... ???
 
Manjusha Muraleedas
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ernest.. I got the point now..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic