• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, but what i understood from the discussion is that, the code:
will work fine in JDK 1.4... ???
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ernest.. I got the point now..
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79179
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic