Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Naresh Shanmugam
Ranch Hand
Posts: 90
  • 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: 90
  • 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
Sheriff
Posts: 24217
38
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: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ernest.. I got the point now..
 
kishan Jaiswal
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
List<String> lst=new ArrayList()
difference with
List<String> lst=new ArrayList<String>()


How JVM internally handle this two.
Why generic are called compile time ?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16026
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Kishan, welcome to the Ranch.

ArrayList without the generics is called a raw type. Raw types exist in Java only because of backward compatibility. When a new version of Java comes out, Oracle is always extremely careful to make sure that code written for the previous version still works without changes on the new version.

In Java 5, generics were added to Java. But there was at that point already a lot of code written without generics, and it was very important that this continued to work. So it is still possible to write code without using generics. You should not do this, however, when you write new code. It is only necessary for backward compatibility.
kishan Jaiswal wrote:Why generic are called compile time ?

When the Java compiler translates your source code to byte code, it uses the generics in your code to check if you didn't make any mistakes with the types (for example, putting the wrong kind of object in a List<String>). But the generics are not translated into byte code - the byte code that is produced contains no information about the generics that you used.

So, generics are only important at compile time (when the compiler is translating your code) and not at runtime (when the JVM is running your program).

kishan Jaiswal wrote:How JVM internally handle this two.

Therefore, there is no difference between these two at the JVM level - the byte code for both of these two lines is the same.

Note that since Java 7 you can write this:

Notice the <> on the right side. This is shortcut syntax, so that you don't have to repeat the <String> that you already wrote on the left side. So, this syntax is exactly the same as:

but just with a little shorter syntax.
 
kishan Jaiswal
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
//how jvm know whether he has to get the string or any other type if the generic information get removed in bytecode
 
Campbell Ritchie
Marshal
Posts: 55672
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kishan Jaiswal wrote:. . . how jvm know whether he has to get the string or any other type if the generic information get removed in bytecode
I moved that question out of the code tags in yuor post as the line would otherwise be too long.

Now, let's look at the bytecode, which you can do with javap:-Note the raw type is shown as unsafe. Note the line starting 36: shows you are implicitly casting the contents to String. That is what happens when you have a List<String>: all its elements are implicitly cast to Strings, and that is how generics is implemented in the background. There is no record in the Class<RawTypeDemo> object of String anywhere, but the bytecode contains lines representing a cast to (String) on the List's elements. That was using code very similar to what you posted.
 
Campbell Ritchie
Marshal
Posts: 55672
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now, you can assign your raw type to a plain simple List reference (line 14) and now you can add any type of Object to it. Try compiling that; you will get lots of compiler warnings especially if you write
javac -Xlint:unchecked RawTypeDemo.java
to get more details. Run this code with
java RawTypeDemo Kishan Jesper Campbell
or similar. I shall leave you to guess what will happen when you run line 16. I shall also leave you to explain why line 13 didn't produce a printout on one of the occasions when I ran such code.
 
kishan Jaiswal
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I execute the given code that you provide Campbell Ritchie and what I found is line 13 will simply print the words (of String type list) in a sop statement
and I understood that line 16 will definitely  produce an error because integer cant not be cast into the string.
 
Campbell Ritchie
Marshal
Posts: 55672
161
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually line 13 prints the List using its toString method, and it would be better to say that line 16 causes an exception to be thrown.
The problem with the raw type here is that it can be assigned to the wrong sort of reference and goodbye type‑safety.
 
Campbell Ritchie
Marshal
Posts: 55672
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The printing to System.out in line 13 and the exception message to System.err in line 16 may be printed by separate threads, and those threads may run at different speeds. If there is a delay in reaching System.out, the exception may be printed and the “main” th‍read terminated before the System.out.println call is completed. So on one occasion I only saw the exception message, not the contents of the List.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!