Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

generics what's the difference?

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is the difference between these two statements

ArrayList<String> list3 = new ArrayList();
ArrayList<String> list4 = new ArrayList<String>();
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First one gives unchecked warning at compile time.
Second statement compiles without warning
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


ArrayList<String> list3 = new ArrayList();
ArrayList<String> list4 = new ArrayList<String>();



list3 is typesafe ArrayList ref variable parameterized with String, of course
will cause compiler warning because it is attempt to assign type unsafe
object to type safe ref variable.

list4 is fine.


Thanks,
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ranchers,


ArrayList<String> list3 = new ArrayList();
ArrayList<String> list4 = new ArrayList<String>();

I don't want to talk about the second line of code, because it is perfect.

But the first line:
ArrayList<String> list3 = new ArrayList();

You can write code like this, it compiles (with a warning) and runs perfectly.
Since the type of list3 is ArrayList<String> you can add Strings to and retrieve Strings (and only Strings) from it.
Trying to add something else (e.g. an Integer or an Object) will result in compile time error. So we are on the safe side, there will no clast cast exceptions at runtime, because adding wrong things would simply not compile.

So why the warning?
The situation of the line occurs in reassigning variable list3. The line (as it is) is a bit unnatural, because nobody would write such a code. But a similar situation arises when you use old code without generics ("legacy code"). Imagine a method that returns you an ArrayList. Not an ArrayList<String>, just a raw type ArrayList. And you type:
ArrayList<String> list5;
list5 = theMethodThatReturnsMeAnArrayList();


In this case the compiler cannot check for type safety. It cannot know if the list is ok, because what the method returns is a raw type ArrayList. So the compiler warns you:"Hey, Chris, the method returns a list that may contain something else than Strings. Please go into the API of that method and assert, that the list the method returns contains only Strings, nothing else!".


Small simulation:



if you paste this code in a main method of a test class of your choice and compile it with
javac -Xlint:unchecked YourFile.java
you'll get a warning about type safety in the indicated line. You also get warnings in the lines where you add something to the legacyList (but I was assuming, this is from old code, somebody has already compile in Java 1.4 or lower).

But it compiles. It compiles and you are warned, that it is not type safe.
You can run it, it will produce a class cast exception in the second for-loop.
But you were warned. If the old code didn't add an Integer to the list, there would have been no exception at runtime.

That's the difference between your lines.



Yours,
Bu.
 
Hang a left on main. Then read this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic