• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

What is difference between new ArrayList<>() and new ArrayList() ?

 
Ranch Hand
Posts: 221
27
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is written on page 130 of Java OCA 8 book

Java 5 introduced generics, which allow you to specify the type of class that the ArrayList will contain.
Java 5 allows you to tell the compiler what the type would be by specifying it between < and >. Starting in Java 7, you can even omit that type from right side. The < and > are still required, though.


If we remove <> from the right side the code does compile too. I couldn't find any difference and I want to know what is the difference between line1 and line2 in the following code?

 
Ranch Hand
Posts: 472
10
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when you use ArrayList<String> list6 = new ArrayList(); - this is only for old code work.
when you compile it you get warning (not compilation error)

uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.


lest make some modifies:
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mushfiq Mammadov wrote:I couldn't find any difference and I want to know what is the difference between line1 and line2 in the following code?


Before Java 5 generics didn't exist at all. So you created an ArrayList as followsThis code has several issues:
  • you can add anything to list (strings, integers, cats, dogs,...), so no type checking by the compiler
  • if you get an element of list, its type is always Object. Even if it was guaranteed to be a list with only strings, list.get(0) would return an Object (the actual object is of course a String). So if you wanted to invoke the length method, you had to cast to String first (and you know casting could be dangerous)

  • Some code to illustrate the aboveAs you can see, it was a lot harder to make sure a list contained only strings. And even if it was such a list, you had to write some lines of code to treat the elements as strings instead of objects. Therefore adding generics to Java was a huge improvement! Code becomes smaller, easier to read and you get type checking at compile time.

    That was a little history lesson! Now back to your original question. If you write the list will work exactly the same as when you would have used ArrayList<String> list5 = new ArrayList<String>(); or ArrayList<String> list5 = new ArrayList<>();. But the compiler will produce a warning because you are mixing generic with non-generic code. Because the type of the reference variable is ArrayList<String>, you'll get the type checking at compile time.
    If you do it the other way round (ArrayList list5 = new ArrayList<String>(); or ArrayList list5 = new ArrayList<>();), the code will still compile (with the same warning about mixing generic with non-generic code). But now list5 can hold any object (strings, integers, cats, dogs,...), because type of the reference variable is ArrayList and thus no type checking occurs at compile time (as in pre Java 5 code).

    Hope it helps!
    Kind regards,
    Roel

    (Disclaimer: for the OCAJP exams you don't have to worry about combining generic with non-generic code, it's not on these exams. But for the OCPJP exam you have to know all little intricacies about combining generic with non-generic code)
     
    Mushfiq Mammadov
    Ranch Hand
    Posts: 221
    27
    IntelliJ IDE Spring Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Sergej Smoljanov wrote: when you compile it you get warning (not compilation error)

    uses unchecked or unsafe operations.
    Note: Recompile with -Xlint:unchecked for details.


    I often get this warning in console, but I don't know it so far. Thanks, Sergej

    Roel De Nijs wrote: If you do it the other way round (ArrayList list5 = new ArrayList<String>(); or ArrayList list5 = new ArrayList<>();), the code will still compile (with the same warning about mixing generic with non-generic code). But now list5 can hold any object (strings, integers, cats, dogs,...), because type of the reference variable is ArrayList and thus no type checking occurs at compile time (as in pre Java 5 code).


    I don't know this way is possible, thanks for your explanation, Roel
     
    Stinging nettles are edible. But I really want to see you try to eat this tiny ad:
    Smokeless wood heat with a rocket mass heater
    https://woodheat.net
    reply
      Bookmark Topic Watch Topic
    • New Topic