Win a copy of Rust Web Development this week in the Other Languages 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

How does the compiler generates the warning when we mix the generic and non-generic code?

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


i don't understand this...means how does compiler produces the warning??

i think..

1. In line 1

ArrayList object can take the object of any type..if we give the reference with generic than there should not be any problem..but there is...

2. In line 2

ArrayList object can take the object of any type. if i refer this object with non-generic which can add any thing..the compiler must tell us that it can add anything to your list...but the compiler didn't...

both of my concepts are failed here..so,how does the compiler produces the warning??
and which thing compiler wants to tell us??


 
Sheriff
Posts: 9691
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
As you might know, generics helps in compile time type safety. So when you declare an ArrayList like this
The compiler must make sure that this list contains elements of only String type. But when you assign it a raw ArrayList, then the compiler can't assure you if the raw ArrayList has only elements of type String. So the compiler cannot guarantee compiler time type safety.
Since after you assign a raw list to a typed list, the compiler can't assure type safety, so the compiler generates a warning at line 3 to inform you that the assignment might result in breaking of type safety. If you look at this the opposite way, assigning a typed list to a raw list doesn't have any problems
The problem actually comes when you add an element to the raw list, at that point type safety might break. This is why the compiler warns you when you add elements to a raw list
 
Sumit Khurana
Ranch Hand
Posts: 68
MyEclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Ankit for your excellent post.I must appreciate your explanation because you made it to easy for me to understand.now i am cleared with this thing.

are there any more warnings which i have to study for the exam??
 
Ankit Garg
Sheriff
Posts: 9691
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
I can't think of other unsafe operations generating a warning which are covered in the SCJP exam. You'll have to wait for someone else who has freshly read the topic from a book
 
reply
    Bookmark Topic Watch Topic
  • New Topic