• 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

How the casting works for classes.

 
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How the casting works for classes. In the cases of primitves, i excepects it will as follows,
float f = 444.444
int i;
When assigning f to i by explicit casting i.e., i = (int) f, the most 32
insignificant bits will be assigned to i, bec i can hold at max 32 bits.
Is something similar to above will happen when casting for classes.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Casting Classes is different. I can only cast an object to a class type if the object follows the same template as the new class type. For instance:
MyClass extends Frame
Frame extends Container
Container extends Component
Component extends Object
NewClass extends Frame
...
I can cast MyClass to Frame, because MyClass necessarily follows the Frame template, and so on up to Object. Similarly I can cast NewClass the same way up through Object, but can not cast to MyClass since they are not directly related (do not pocess the same template defined in the Class file).
I can cast down from Object to classes lower in the class heirarchy only if they match the same template for the lower class. This should always be tested before by doing:
Object o = getMyObject();
MyClass mc;
if (o instanceof MyClass)
mc = (MyClass) o;
Casting doesn't reasign any values, it simply tells the JVM to look at a different template when accessing the data stored in the variable. So you can only cast to data types that the stored data can fit...
Hope that wasn't too long winded, and helped.
Steve
 
Ranch Hand
Posts: 319
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That was a neat explanation Steve! Thanks.
sudharsan
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When assigning f to i by explicit casting i.e., i = (int) f, the most 32 insignificant bits will be assigned to i, bec i can hold at max 32 bits.
This is not a correct understanding of what occurs when casting a float value to an int. The data type float is 32 bits, as is the data type int. A different style of bit pattern is used for floating point data types than is used for integral data types. When casting from float to int, the integer part of the float value is simply used to create the int value.
Also note that the floating point literal 444.444 would be considered to be of type double in Java.
 
reply
    Bookmark Topic Watch Topic
  • New Topic