This week's giveaway is in the Testing forum.
We're giving away four copies of TDD for a Shopping Website LiveProject and have Steven Solomon on-line!
See this thread for details.
Win a copy of TDD for a Shopping Website LiveProject this week in the Testing 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Q about generic compiler warnings

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


When I run this from the CMD first line compiles with warnings ,second compiles without (although they both produce warnings on eclipse).I am very confused because the first declaration of ArrayList can only take Strings ,but second one can take any type of object which will make a mess at runtime so shouldn't it be the other way round? Could you please explain to me this issue because I'm so confused and please correct me if I'm wrong.
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am a little confused about what you are trying to ask. Are you asking if line 1 or line 2 should not compile or if one of them should not throw a warning message ?
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

S Ali wrote:
When I run this from the CMD first line compiles with warnings ,second compiles without (although they both produce warnings on eclipse).I am very confused because the first declaration of ArrayList can only take Strings ,but second one can take any type of object which will make a mess at runtime so shouldn't it be the other way round? Could you please explain to me this issue because I'm so confused and please correct me if I'm wrong.



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

second one can take any type of object which will make a mess at runtime so shouldn't it be the other way round?


I'm able to add any object to this list : List l2 = new ArrayList<String>();
And it does not give any runtime exceptions also. Then what is the effect of initializing ArrayList<String>(). It is the same as without the generic declaration. Am I right?
 
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What exactly is a generic.....type safety and what is the broader class for a generic type that is a raw type.
Generic instantiation assigned to a raw reference type. This compiles without warnings as the compiler knows only about the reference type. I will create a class cast exception at runtime if the programmer has a cast like (Integer)l2. This is allowed at compile time as l2 is a raw type and the list may contain Integer which is not known to the compiler.


This following can be seen as unsafe as the arraylist can contain many other things but the compiler allows only String.


This code can be viewed as in


First the compiler is upset that you are assigning a raw type to a generic type but the compiler cannot do anything but give a warning because both the classes are the same after type erasure.


 
Ranch Hand
Posts: 449
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Murtuza Arzai wrote:
I'm able to add any object to this list : List l2 = new ArrayList<String>();
And it does not give any runtime exceptions also. Then what is the effect of initializing ArrayList<String>(). It is the same as without the generic declaration. Am I right?



Yes. If you declare List l2 = new ArrayList<String>() then you lose all the safety benefits provided by generics. The row types are only allowed because of compatibility. In general, you should never use row type such as List in your code.
 
Sheriff
Posts: 9692
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The compiler will issue a warning only it thinks that it can't ensure type safety (as Nitish mentioned). The warnings are issued if some of the statements might result in a run-time exception (and thus break type safety). Let's take the statements one by one



Here the compiler issues warning. As Anastasia explained this is because the right hand operand might contain a List of any type of objects (like below).



After this point it is the responsibility of the compiler to ensure that l contains only elements of type String. But since the list might already contain a different type that String (like in the second code), thus later a get() operation on the typed list might result in a ClassCastException (thus break type safety). This is why the compiler issues a warning as this statement might break type safety



Now lets examine the other statement



This time you don't get any warning. This is because you are just assigning the typed list to an untyped list. You'll only get a warning if you try to add anything to l2. This is because that might break type safety somewhere else



This is why the compiler issues a warning when you try to add an element to an untyped collection...
 
S Ali
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to you all for clarifying this to me ,especially Ankit I totally got your explanation.
 
This will take every ounce of my mental strength! All for a tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic