• 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
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Type Assigning

 
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone help me understanding type assigning and erasure in the following code, and why only line24 throws class cast exception not line21. Its a continuation of previous post, which I cud not follow much.

 
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you can follow this link where discussion on same doubt is going :Generic type casting
 
Himalay Majumdar
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have already been to that post. This post infact is because of the one you pointed to me.
I needed a simpler explanation and hence posted relatively a simple example.

I am still waiting for a better explanation from someone.
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because when you concatenate two String objects, the compiler avoids calling toString() on the second object and performs some code specific to String + String concatenation, so in that case a cast to String (which will fail in this case) is performed. But for all other types toString() is called on the second object, and since toString() is defined in Object, the compiler doesn't bother with the casting to String, and simply casts to Object, which will succeed (since all objects are instances of Object.)
 
Himalay Majumdar
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ruben..can you please tell how the code will look after type erasure.
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Himalay Majumdar wrote:Hi Ruben..can you please tell how the code will look after type erasure.


Hi Himalay,

It will look something like this:

 
Himalay Majumdar
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I knew you tried explaining the String behaviour before..which i cud not follow.
Can you please explain me why only cast to String is performed in Line 11 of your code.

I dont see any concatenation(apart from the plus sign) here..

System.out.println("1) " + (String) box2.u);

i mean System.out.println(box2.u); produces the same error.
 
A teeny tiny vulgar attempt to get you to buy our stuff
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic