0% found this document useful (0 votes)
2 views

Java Type Casting

Type casting is the process of converting one data type to another, categorized into primitive and derived type casting. Primitive casting includes widening (implicit conversion to a higher data type) and narrowing (explicit conversion to a lower data type, which may result in data loss). Derived casting involves upcasting (converting a subclass reference to a superclass reference) and downcasting (converting a superclass reference back to a subclass reference), with specific rules governing their usage.

Uploaded by

lsrinivas.rpa
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Java Type Casting

Type casting is the process of converting one data type to another, categorized into primitive and derived type casting. Primitive casting includes widening (implicit conversion to a higher data type) and narrowing (explicit conversion to a lower data type, which may result in data loss). Derived casting involves upcasting (converting a subclass reference to a superclass reference) and downcasting (converting a superclass reference back to a subclass reference), with specific rules governing their usage.

Uploaded by

lsrinivas.rpa
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

TYPE CASTING

- Converting one data type to another data type is called as typecasting.


- It is of two types
(i) Primitive Type casting
(ii) Derived Type Casting/ Class typecasting / Object typecasting

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

Primitive Casting is of two types:


(1) Widening
(2) 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

Simple typeCasing Program


class TypeCasting
{
public static void main(String[] args)
{
int a=20;
double b= a; // widening
System.out.println("a = "+a);
System.out.println("b = "+b);

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.

- Type Casting Syntax :


(datatype) value/variables ;
- Narrowing always results in loss of data.

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);

int sum = 66;


int length = 5;

float average=(float)sum/length;
System.out.println("average is "+average);

}
}
o/p – x1=20.0
y1 = 20

- If we directly performing narrowing, then it throws an error in program:

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

- If you try to convert a character to integer then integer variable will be


containing Unicode value for the given character.
Program
class CharacterTypeCasting
{
public static void main(String[] args)
{
char c1 = 'A';
int i = c1; // widening
System.out.println("c1 = "+c1);
System.out.println("i = " + i);

int j = 77;
char c2 = (char)j; // narrowing
System.out.println(" j = " + j);
System.out.println( " c2 = " + c2);
}
}

- For a method expecting primitive value we can pass the same


primitive datatype value and all its lower primitive datatype value.

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);

}}

- For a method expecting primitive value then if we pass the higher


primitive datatype value then it throws an error.
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)
{
double d = 10.5;
test(d);
long l = 25.3;
test(l);
}
}
- If a class contains two overloaded methods one expecting lower
datatype value, other expecting higher datatype value and if you pass a
lower datatype value to call the method then always method with
lower datatype value will be executed.
Program
class Over
{
public static void test(int x )
{
System.out.println("This is test(int x) ");
System.out.println("print x = " + x);
}
public static void test(double x )
{
System.out.println("This is test(double x)");
System.out.println("print x = " + x);
}
public static void main(String[] args)
{
int v1=10;
test(v1);
}
}
// op : this is test(int x )
// print x = 10

Derived Casting or Class Typecasting or Object TypeCasting

- Converting one reference type to another reference type is called as


derived/class typecasting.
- Derived Casting is of two type :
(i) Upcasting
(ii) Down casting
Upcasting
- Converting subclass or childclass reference to superclass or parent class
reference is called as upcasting.
- create an object of sub class and store it into a reference of super is called as
upcasting.

- 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

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
//downcasted object
Admin a1 = (Admin) s1; // Admin a1 = new Admin()
A1.edit();
A1.view();
}
}
==============
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();
Subclass sub1 = (Subclass)sup1; // downcasting,
done by writing downcasting statment
sub1.test();
sub1.view();
}}
- We can downcast only upcasted reference. If we downcast
directly superclass into subclass then JVM throws an
exception as ClassCastException at runtime.
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 SuperClass();
Subclass sub1 = (Subclass)sup1; //
downcast directly superclass without upcasting
sub1.test();
sub1.view();
}}

You might also like