Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Mack Wilmot
Ranch Hand
Posts: 88
Linux Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • 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?

 
fred rosenberger
lowercase baba
Bartender
Posts: 12185
34
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.
 
Ranji Sura
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • 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
Linux Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • 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.

 
Campbell Ritchie
Sheriff
Pie
Posts: 49789
69
  • Mark post as helpful
  • send pies
  • 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.
 
Winston Gutkowski
Bartender
Pie
Posts: 10504
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic