Java Type Casting
Java Type Casting
Primitive Casting
- Converting one primitive datatype of another primitive datatype is
called as primitive casting.
widening
long double
int float 64 bits 64 bits
short char
Byte 32 bits 32 bits
16 bits 16 bits
8 bits
narrowing
Widening
- Converting a lower datatype value to higher datatype value is called as
widening.
- Widening will be done by compiler implicitly.
Program
class TypeCasting
{
public static void main(String[] args)
{
int x1=20;
double y1=x1;
System.out.println("x1 = "+x1);
System.out.println("y1 = "+y1);
}
}
o/p – x1=20
y1 = 20.0
double x1=20;
int y1=(int) x1; // narrowing
System.out.println("x1 = "+x1);
System.out.println("y1 = "+y1);
}
}
Op– x1=20
y1 = 20.0
x1=20.0
y1 = 20
Narrowing:
- Converting a higher data type value to lower datatype value is called as
Narrowing.
- Narrowing should be done by the programmer explicitly by writing
casting statement.
Program
class TypeCastingLossOfData
{
public static void main(String[] args)
{
int x1=127;
byte y1=(byte) x1;
System.out.println("x1 = "+x1);
System.out.println("y1 = "+y1);
int a=240;
byte b = (byte)a;
System.out.println("a = "+a);
System.out.println("b = "+b);
}
}
// in the above program while in 1st block it does not loss of data because byte
holds the data upto 128 bytes but in the second block there is loss of data
because we want to store 240 in byte, but the capacity of byte is only 128.
Op :
X1 = 127
Y1= 127
A= 240
B= -16 // here loss of data
Program :
class TypeCasting
{
public static void main(String[] args)
{
double x1=20.34;
int y1=(int)x1;
System.out.println("x1 = "+x1);
System.out.println("y1 = "+y1);
float average=(float)sum/length;
System.out.println("average is "+average);
}
}
o/p – x1=20.0
y1 = 20
class TypeCasting
{
public static void main(String[] args)
{
double x1=20;
int y1=x1;
System.out.println("x1 = "+x1);
System.out.println("y1 = "+y1);
}
}
//it throws an error as possible loss of precision
int j = 77;
char c2 = (char)j; // narrowing
System.out.println(" j = " + j);
System.out.println( " c2 = " + c2);
}
}
Program
class Mainclass13
{
public static void test(int x )
{
System.out.println("this is test(int x) of sample");
}
public static void main(String[] args)
{
byte s = 10;
test(s);
short a = 52;
test(a);
int b=25;
test(b);
}}
- During up-casting only super class behavior is visible, sub class behavior is hidden.
Program
class SuperClass
{
public void test()
{
System.out.println("This is test() ");
}
}
class Subclass extends SuperClass
{
public void view()
{
System.out.println("This is view()");
}
}
class Upcasting
{
public static void main(String[] args)
{
SuperClass sup1 = new Subclass(); //Upcasting
sup1.test();
or we can also write like this
Subclass sub1 = new Subclass();
SuperClass sup2=sub1; // Upcasting
sup2.test();
}
}
=========
class Student //Ex-1
{
public void view()
{
System.out.println("Student details are :");
System.out.println("Name:John P");
System.out.println("MJCET");
System.out.println("BTECH-Computer Science");
System.out.println("Degree-58.3%\nINT-75%\nSSC-66%");
}
}
class Admin extends Student
{
public void edit()
{
System.out.println("Edit details of Student");
System.out.println("Name:John P");
System.out.println("MJCET");
System.out.println("BTECH-Computer Science");
System.out.println("Degree-60.9%\nINT-75%\nSSC-66%");
}
}
public class App
{
public static void main(String args[])
{
//upcasted object
Student s1 = new Admin();
s1.view();
//s1.edit();//CTE
}
}
- Using upcasted reference variables can access only superclass or
parent class property.
Program
class SuperClass
{
public void test()
{
System.out.println("This is test() ");
}
}
class Subclass extends SuperClass
{
public void view()
{
System.out.println("This is view()");
}
}
class Upcasting
{
public static void main(String[] args)
{
SuperClass sup1 = new Subclass();
sup1.view(); /* here it throw an error as cannot find the
symbol because Superclass reference can only access the
superclass method not a subclass method. */
}
}
- Upcasting will be done by the compiler implicitly.
- Upcasting is achieved by storing the address of child class object into
parent class reference variable.
SuperClass sup1 = new Subclass();
In the above line sup1 is the reference variable which store
the address of Sublclass object
Downcasting
- Converting the upcasted reference back to subclass reference is called as
downcasting.
- Downcasting should be done by the programmer explicitly by writing casting
statement.
- Converting a super class object type into sub class object type is called as
downcasting.
- (or)
- Converting an upcasted object into normal form is called as downcasting.
- During downcasting both sub class and super class behaviour is visible.
- Since, super class does not contains property of sub class directly, downcasting is
not possible, explicitly we have to convert it.
- Note: During upcasting some properties are hidden, during downcasting all
properties are visible.
-
Program