Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Mushfiq Mammadov
Ranch Hand
Posts: 187
25
Java jQuery Netbeans IDE
  • Mark post as helpful
  • send pies
  • 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?

 
Sergej Smoljanov
Ranch Hand
Posts: 467
10
  • Likes 2
  • Mark post as helpful
  • send pies
  • 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:
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 2
  • Mark post as helpful
  • send pies
  • 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: 187
    25
    Java jQuery Netbeans IDE
    • Mark post as helpful
    • send pies
    • 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
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic