aspose file tools*
The moose likes Beginning Java and the fly likes How does the run time know a field is a Double if compile time does not know? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How does the run time know a field is a Double if compile time does not know?" Watch "How does the run time know a field is a Double if compile time does not know?" New topic
Author

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

Mack Wilmot
Ranch Hand

Joined: Jul 27, 2011
Posts: 88

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

Joined: Oct 02, 2003
Posts: 11314
    
  16

the object I see you creating on line 29 is a Double. the reference type you use doesn't change what the object is.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Ranji Sura
Ranch Hand

Joined: Oct 28, 2012
Posts: 84
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

Joined: Jul 27, 2011
Posts: 88

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

Joined: Oct 13, 2005
Posts: 38865
    
  23
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

Joined: Mar 17, 2011
Posts: 7807
    
  21

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

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How does the run time know a field is a Double if compile time does not know?