View RSS Feed

Core Java

Type casting

Rate this Entry
by , 01-08-2012 at 12:50 PM (1986 Views)
When you treat a variable of one type to another type, it is called type casting. Up casting supports automatic conversion but in case of low casting you need to explicitly specify it.

Java Code: Type Casting
byte -> short -> int -> long -> float -> double
int i = 5;
long j = i; //Right. Up casting or implicit casting
byte b1 = i; //Wrong. Compile time error “Type Mismatch”.
byte b2 = (byte) i ; //Right. Down casting or explicit casting is required.
In case of objects, you can cast a subclass object into a superclass.

Name:  61.JPG
Views: 741
Size:  34.6 KB

Upcasting vs Downcasting

Submit "Type casting" to Facebook Submit "Type casting" to Digg Submit "Type casting" to Submit "Type casting" to StumbleUpon Submit "Type casting" to Google

Updated 01-14-2012 at 07:46 PM by Core Java