Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
6 views4 pages

5 Aug 2022 Typecasting

Type casting is the process of converting one data type into another, categorized into implicit and explicit casting. Implicit casting, or widening, occurs when converting smaller data types to larger ones without loss of information, while explicit casting, or narrowing, involves converting larger data types to smaller ones, which may result in data loss. If a source variable's value exceeds the destination variable's range during casting, the value is adjusted to fit within the range without causing errors.

Uploaded by

anjaliaryan127
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)
6 views4 pages

5 Aug 2022 Typecasting

Type casting is the process of converting one data type into another, categorized into implicit and explicit casting. Implicit casting, or widening, occurs when converting smaller data types to larger ones without loss of information, while explicit casting, or narrowing, involves converting larger data types to smaller ones, which may result in data loss. If a source variable's value exceeds the destination variable's range during casting, the value is adjusted to fit within the range without causing errors.

Uploaded by

anjaliaryan127
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/ 4

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