hq78 wrote:
Can someone tell me what Typecasting and Promotion are in Ready To Program Java IDE by holtsoftware?
Casting is aking an object of one class and forcing the environment to regard it as an object of another class. Let's ignore primitives (int, float, boolean, etc) for now.
Every object, due to inheritance, is an "Object", so we can do something like:
code: |
Object foo = new Integer(5); |
However, when we do this, we can only treat the variable "foo" as a relatively limited object of type Object. But, we can use a cast to "turn" that object back into an Integer.
code: |
Integer bar = (Integer)foo; |
We're "promoting" foo to an Integer from an Object.
Of course, you can't just cast foo to anything... if you try to, an exception will occur.