Win a copy of Spring Boot in Practice this week in the Spring 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:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

How does the run time know a field is a Double if compile time does not know?

 
Ranch Hand
Posts: 88
Netbeans IDE Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

In [line 39], the value of ob inside raw is obtained, and this value is cast to Integer. The trouble is that raw contains a Double value, not an integer value. However, this cannot be detected at compile time because the type of raw is unknown. Thus, this statement fails at run time.



Question: How does the run time know ob is a Double if ob was created type Object? If it was created a Double, why does the compile time not know it?

 
lowercase baba
Posts: 13063
67
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the object I see you creating on line 29 is a Double. the reference type you use doesn't change what the object is.
 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Question: How does the run time know ob is a Double if ob was created type Object? If it was created a Double, why does the compile time not know it?



Because , it is a run time exception.

Have you seen the exception occured at runtime ? It is "ClassCastException" , it is a sub type of "RuntimeException" class . RuntimeException or it sub classes are compile-time unchecking exception. If you go through the API , I think you will find the answer yourself.



 
Mack Wilmot
Ranch Hand
Posts: 88
Netbeans IDE Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess it is not any different than assigning this Double to an Object reference. In the context of Generics it confused me.

 
Marshal
Posts: 76110
362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A Double is an Object, as you already know. Remember all objects maintain a reference back to their Class<T> object which they were created from; this allows the runtime to work out their actual type.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mack Wilmot wrote:I guess it is not any different than assigning this Double to an Object reference. In the context of Generics it confused me.


You're not the first. Simply put, generics doesn't exist at runtime (not quite true, but close enough for now); it is strictly a compile-time check.

If you create a generic class, you should always supply a type when you create it. If you don't you'll get a compiler warning which is basically saying:
You've created a generic class, but you haven't supplied me with a type, so all your compile-time checking has just gone out the window.

For more information, I suggest you read the tutorial. And you may have to read it more than once, because some of it isn't simple.

Good luck.

Winston
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic