• 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

Mixing Generics and Legacy code question

 
Greenhorn
Posts: 4
Mac OS X VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Output: hello

I understand that Legacy List can add any Object. But after coming out of function, why is able to get String and print out (while it is )


[HENRY: enabled code tags]
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

As you already noticed, don't mix generics with non-generics -- generics can break.

Henry
 
Rahul Spark
Greenhorn
Posts: 4
Mac OS X VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. Can you please explain why it is not giving ClassCastException while printing String from List<Integer>?

My question is more for certification point of view and for better understanding this. Thanks again.
 
Saloon Keeper
Posts: 14505
325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The compiler expects to retrieve an Integer, for which the PrintWriter has no special method, so the compiler chooses the println() method that has the Object argument, which works equally well for the String retrieved.
 
Rahul Spark
Greenhorn
Posts: 4
Mac OS X VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Stephan

Infact below code gave ClassCastException

 
Stephan van Hulst
Saloon Keeper
Posts: 14505
325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, because it tries to cast the reference to an Integer, which it's clearly not.

In the former case, it casts the reference to an Object. This is perfectly fine.
 
Thanks tiny ad, for helping me escape the terrible comfort of this chair.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic