0% found this document useful (0 votes)
23 views10 pages

Type Casting

The document discusses type casting in Java, including widening and narrowing conversions between primitive data types. Widening conversions can be done implicitly by the compiler but narrowing requires explicit casting. Common casting examples are provided between integer, floating point, character, and boolean types.
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)
23 views10 pages

Type Casting

The document discusses type casting in Java, including widening and narrowing conversions between primitive data types. Widening conversions can be done implicitly by the compiler but narrowing requires explicit casting. Common casting examples are provided between integer, floating point, character, and boolean types.
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/ 10

Type Casting

Type Casting:
It is process of converting data from one type into another type is
called as type casting

1010.0(Converting data from int to double)


int double

‘A’65(Converting data from char to int)


char int

o Type casting can be performed by programmer(Explicitly) and by


compiler(implicitly)

Type casting can be done by

Programmer Compiler
(Explicitly) (Implicitly-during compile time by
compiler)

(Can’t store)

(Can store) Ice cube


(Convert into water to store)

Bottle

Water

From the about example we are changing type of material that we store
but not type of container

int a; a
We had created a container ‘a’ that can only store int type of data
int a=10; a 10
//int = int
(As we are creating int type of container we can only store int type of data)

int a=10.0;
int = double X(Not possible)
(As java is strightly typed language If we try to store double type data inside int
type container we will get compile time error as “incompatible”
if we want to store then we need to change the type of data but not type of
container)

Type casting is divided into 2-types


1)Primitive Typecasting
2)Non-primitive Typecasting

Type casting

Primitive Typecasting Non-primitive Typecasting

Widening Narrowing Upcasting Downcasting


(Implicitly, (Explicitly) (Implicitly, (Explicitly)
Explicitly) Explicitly)

Primitive Type-Casting:
The process of converting one type of primitive data into another type is
called as primitive type casting
o We have 8-primitive datatypes with their sizes in bytes
1. byte -1
2. short -2
3. int -4
4. long -8
5. float -4
6. double-8
7. char-2
8. boolean

Primitive data is classified into 2-types

1)Widening(Converting smaller range data into larger range)


2)Narrowing(Converting larger range data into smaller range)

Arranging data-types according to sizes:

Widening

byte<short<int<long<float<double
1 < 2 <4< 8< 4 < 8

Narrowing

Widening: (Real time example: Shifting from small house to huge)

The process of converting smaller range(size) data into larger range


data is known as widening
o There is no data loss
o Compiler can do widening implicitly(As there is no data loss)
o We can’t convert boolean into any other type
Program:

1.class TypeCasting {
public static void main(String[] args) {
int res=10; //size of int is==>4
long l=res; //size of long is==>8 //Storing smaller range data into
//large container(Widening,
//No data loss)
//conversion done by compiler
System.out.println(l);
}
}
Output:
10

Task:
Fill the table below with YES/NO, Observe the output

if we can performing widening then fill with YES else fill with NO

Container/Variable type
Data byte short int long float double char boolean
byte
short
int
long
float
double
char
boolean

Ex:

Container/Variable type
Data int

short YES/NO

Take short type of data and place it inside int type of container

class Demo{
public static void main(String[ ] args )
{
short s=40;
int i=s;
Syste.out.println(i);

}
}

Narrowing: (Real time example: Shifting from large house into small)
The process of converting larger range(size) data into smaller range
data is known as Narrowing
o There is possibility of data loss
o Narrowing can‘t done by compiler implicitly(As there is chance of
data loss)
o We/Developer need’s to convert explicitly

Program:

class TypeCasting {
public static void main(String[] args) {
double res=10;//size of int is==>8
long l=res;//size of long is==>4
//Storing larger range data into
//small container(Narrowing,
//Possible of data loss)
//conversion can’t done by compiler

System.out.println(l);
}
}

Output:
TypeCasting.java:5: error: incompatible types: possible lossy conversion from
float to long
long l=res;//size of long is==>8
^
1 error

To convert larger range data to smaller range data (Narrowing) we will take help
of “type casting operator” for explicit conversion
Typecasting:
o It is a unary operator which accepts only one operand as input
o It is used to convert larger range data to smaller range data
(Narrowing) Explicitly (by developer/programmer)
o Explicit TypeCasting is nothing but programmer is forcefully
converting from one type of data to another type of data with the
help of TypeCasting operator

Syntax:
(type) value/variable/expression;

Program:

class TypeCasting {
public static void main(String[] args) {
double res=10.0;//size of int is==>8
long l=(long)res;//size of long is==>4
//Using TypeCast operator
System.out.println(l);
}
}

Output:
10

Program :

class TypeCasting2{

public static void main(String [ ] args)


{

int numInt = 10;


double numDouble = numInt;
System.out.println(numDouble); // Implicit casting from int to double(Widening)

}
Output:
10.0

Program :
class TypeCasting{

public static void main(String [ ] args)


{

double numDouble = 3.14;


int numInt = (int) numDouble; //Explicitly casting double to
//int(Narrowing)
System.out.println(numInt);

Output:
3

Note :
We can also perform widening(Which is implicitly done by compiler) using
TypeCast operator (Explicitly)

Program : (Supporting above note)

class TypeCasting{

public static void main(String [ ] args)


{
int a=10;
float f=(int)a; //Widening by using typecast operator
System.out.println(f);
}

}
Output:
10.0

Programs: (Using TypeCasting Operator)

Program-1:
class TypeCasting{

public static void main(String [ ] args)


{
System.out.println((int)25.2);
}

Output: 25

Program-2:

class TypeCasting2{

public static void main(String [ ] args)


{
System.out.println((int)’A’);
}

Output: 65

Task:
Now fill the table below by using TypeCasting operator only

Container/Variable type
Data byte short int long float double char boolean
byte
short
int
long
float
double
char
boolean

You might also like