• 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
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

What should be the o/p?

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Strings{
public static void main(String[] args) {
ArrayList<Integer> lst = new ArrayList<Integer>();
ArrayList lst1 = lst; // LINE 1
lst.add(1);
lst1.add("string");
lst1.add(true);
}
}

I expect a "warning message" LINE 1. But there is no such message. Any reasons why ???
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For this you have to import java.util package.
I checked and compiled this program.. i got two warnings..

they are:

Note: Strings.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
 
Ebenezer Samuel
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is the above you warning are your expecting..? If not please kindly ignore this..
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy ranchers,

you won't get a warning when you assign a generic object to a non-generic variable. Only the other way round:


Because in line 1, a list that contains only strings is refered by a raw type variable containing objects. String to object is fine.
But on line 2 you may refer a list that may contain anything to a variable that is supposed to refer only to lists of type string. Maybe dangerous, so it compiles but with a warning.


Yours,
Bu.
 
kishore Kumar Sangam
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Hassel,
If you analyze the code above

After assigning the generic to non-generic reference, I am adding objects that are not Integer type using the non-generic reference.

That is also dangerous rt!!!
 
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
Yes, you could consider that "dangerous". However, note that you will not get a compiler or runtime error (exception) when you do this. This is perfectly legal:

That is because of type erasure: the compiler doesn't keep the element type with the list in the class file. So at runtime, list 'a' looks like a plain List, the JVM will not know that it's actually a List<String> and it will not check at runtime that you're adding something else than a String to it.

Note that if you try to cast back 'b' to a List<String>, you do get a compiler warning, as already said above.
 
kishore Kumar Sangam
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jepser,
My question is not on errors.It is on warnings, When you assign Non-Generic to Generic compiler warns you becoz it is dangerous. But the other way round the compiler dosen't.

My question is why it dosen't warn ? The above code snippet proves that the other way round can also be dangerous.

Thanks
Kishore
 
Jesper de Jong
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
Because assigning from a generic to a non-generic type is not really dangerous, as my code snippet demonstrated. Only the other way around is dangerous (non-generic -> generic type).
 
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
If you are converting from generic to non- generic and adding something with non-generic reference then you will get warning
if you are not adding anything then you will not get warning
 
CLUCK LIKE A CHICKEN! Now look at this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic