0% found this document useful (0 votes)
19 views9 pages

Type Casting

Uploaded by

Ratan Thakur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views9 pages

Type Casting

Uploaded by

Ratan Thakur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

TYPE CASTING

What is type casting ?


• The process of converting one type of data is known as type casting.

• There are two types of type casting.

TYPE CASTING

PRIMITIVE TYPE NON-PRIMITIVE


CASTING TYPE CASTING
Primitive Type Casting

• The process of converting one primitive type value into another


primitive type value is known as primitive type casting.

• There are two types of primitive type casting.

1. WIDENING

2. NARROWING
WIDENING :

• The process of converting smaller range primitive data type into


higher range primitive data type is known as widening.
• In this process, there is no data loss.
• Since there is no data loss, compiler implicitly performs widening.
Hence, it is also called as auto-widening.

Byte < short < char < int < long < float < double
Example for widening :
class Widening
{
public static void main(String[] args)
{
int a=10;
float num=a; // automatically int is converted to float type
System.out.println(a);
System.out.println(num);
}
}
Output :

10
10.0
class Widening
{
public static void main(String[] args)
{
int a=10;
char c=a; // Compile time error
System.out.println(a);
System.out.println(c);
}
}

Note : It will give compile time error since we are trying to convert higher range data
type (int) to smaller range data type( char).
NARROWING :

• The process of converting higher range primitive data type into lower range
primitive data type is known as narrowing.
• In this process, there is possibility of data loss.
• Since there is possibility for data loss, compiler will not perform anything
implicitly.
• The programmer will have to explicitly(manually) perform Narrowing.
• This can be achieved with the help of type cast operator.

Byte < short < char < int < long < float < double
Example :

class Narrowing
{
public static void main(String[] args)
{
int a=10;
char ch=a; // Compile time error
System.out.println(a);
System.out.println(c);
}
}
Note : It will give compile time error since we are trying to convert higher range data
type (int) to smaller range data type( char) without type cast operator.
Type cast operator :
• It is an unary operator.
• It is used to explicitly convert one data type to another data type.
Example for narrowing :

class Narrowing
{
public static void main(String[] args)
{
int a=10;
char ch=( char ) a; //explicitly converting float to int using type cast operator
System.out.println(a);
System.out.println(c);
} Output :
} 10.0
10

You might also like