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

27 May 2021

Type casting involves converting one data type into another. There are two types of casting: implicit and explicit. Implicit casting widens data types with no information loss, such as byte to int. Explicit casting narrows data types, which may lose information, like int to byte. When casting to a smaller range, the value wraps within the new range without errors.

Uploaded by

sandesh ahir
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)
11 views

27 May 2021

Type casting involves converting one data type into another. There are two types of casting: implicit and explicit. Implicit casting widens data types with no information loss, such as byte to int. Explicit casting narrows data types, which may lose information, like int to byte. When casting to a smaller range, the value wraps within the new range without errors.

Uploaded by

sandesh ahir
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/ 5

Understanding Type Casting

➢Converting one data type into another data type


is called casting.
➢In general there are two types of casting

procedures. ✓Implicit Type Casting

✓Explicit Type Casting


Implicit Type Casting:
➢Converting smaller data type to larger data types is called
“Implicit Type Casting”.
➢It is also known as Widening or Casting-Upwards.
➢There is no lose of information in this type
casting.

byte -> short, int, long, float, double


short -> int, long, float, double
char -> int, long, float, double
int -> long, float, double
long -> float, double
float -> double
Explicit Type Casting
⚫ Converting larger data type to smaller data types is called “Explicit
Type Casting”.
⚫ It is also known as Narrowing or Casting-Downwards. ⚫ There
may be a chance of lose of information in this type casting.
<Destination DataType> <variableName>=(DataType)
<SourceType> ⚫ Ex: int i=90;
⚫ byte b = (byte)i;
byte
->
char
short
->
byte,
char
char ->
byte, short
int -> byte, short, char
long -> byte, short, char, int
float -> byte, short, char, int, long
double -> byte, short, char, int, long, float
In casting what happens if source variable has value
greater than the destination variable type range? ⚫ We
will not get any compile time error or runtime error,
assignment will be performed by reducing its value in the
range of destination variable type range.
⚫ We can know the value by using the below formula

[minimumRange + (result - maximumRange -


1)]

You might also like