• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Generics query

 
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In K&B pg 603 , it is explained that when we try to use legacy code (method) which modifies the type-safe list, the complier
shows warnings and runs well without exception. But i noticed that on my compiler( Dr JAVA) i am getting ClassCastException .
Could anyone please tell ??
Here goes the code :-

 
author
Posts: 23951
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
I think you misinterpreted the statement. I believe the book is trying to say that all you will get is a compiler warning -- ie. you will not get any runtime errors about the (due to the) mixing generics and non-generics.

Other runtime errors caused by problems from mixing generics and non-generics is a different story.

Henry
 
Sahil Kapoor
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In accordance to K&B verbatim

pg 602,
.

Will that work? Yes,sadly, it does! It both compiles and runs. No runtime Exception. Yet, someone just stuffed a String into a supposedly type safe
Array List of type <Integer> How can that be ???



Actually i removed Line 1 and Line2 to and it worked fine , ie Runs without Exceptions. So i thought that since i am using it thats why there was an exception
But, then again another brain neuron hits my mind that, i am using Object ref in for loop then there should not be any issue like that.....

Please run the above code in your compiler ....May be some compiler issue ???

Thanks !!!
 
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try the code below and you will see the incompatible type errors.
The List parameter of add2() is local, and previously it was not
declared as type <String>. Jim ... ...
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think it is not due to legacy code; the problem is elsewhere. Here, JVM generates the following exception:

Sahil Kapoor
Varun Kapoor
Exception in thread “main” java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
At TestLegacy.main(TestLegacy.java:14)


From the above for-each loop, we can apparently think that the element of the myList, what is either String or Integer should assign into Object reference normally. But, I feel there is an intermediate step that try to cast the element of the myList into the actual parameter type with which the myList is declared and instantiated. And then the reference of the element is assigned into the Object reference.

Another part of the story is that if we use Iterator<E> instead of for-each loop then we will get the expected result. For instance,


Vary interesting!
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think, there is a internal mechanism in the foreach loop, when we are using it with the Generics. Because, the code below works ...
 
Unmesh Chowdhury
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your case, for-each loop is used for raw type, that is, the list is not generic. So, in this case the compiler doesn't treat the list specially. The context of the previous for-each loop and the context of your for-each loop is not same.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic