• 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

Casting issues

 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, I'm on a posting binge.
I just wanted to get this clear, because after missing this one question on the John Hunt exam, it is turning my world upside down.
Ok implicit casting. If I want to do an implicit casting, the object that I am trying to implicitly cast, must be of the same or smaller type than the object that is to contain it. Meaning
int i = s // a short
int i = c // a char, not even sure if that is possible
short s = b // a byte
but I can use explicit casting to do almost anything I please...meaning the explicit cast will be chopping off information just to make it fit. Like if I tried to do
byte b = (byte)s // as short
the (byte) casting will just chop all the information off the s.
If that is the case, does the explict casting do the same type of "chopping" to objects? Like if I did a Car myCar = (Car)H*nda explicit casting, will myCar just be a Car again, or will "H*nda Information" still linger somewhere, so that if I did a (H*nda)myCar, all the "Honda Information" will come back
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Casting of references does not effect the object pointed to by the reference but it can certainly effect the behavior of the code. The reference type is used to determine what member variables amd static methods to access, and will also determine where the search for an overridden method starts - if the class of the reference does not declare a method of the actual class of the object, then you cannot call that method with that reference without explicitly casting to the subclass (downcasting). Note that no cast is necessary to cast to a superclass - implicit upcasting is allowed. Another difference between explicit casting of primitives to smaller types and reference casting is that primitive casting will not result in an exception, just possible loss of information, but reference casting will give you a ClassCastException if the actual type of the object is not compatible with the reference being cast to.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic