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

Java Type Casting

Java type casting involves converting a value of one data type to another. There are two main types: widening type casting and narrowing type casting. Widening type casting automatically converts a lower data type to a higher one without data loss. Narrowing type casting manually converts a higher data type to a lower one through parentheses, which can result in data loss.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
92 views

Java Type Casting

Java type casting involves converting a value of one data type to another. There are two main types: widening type casting and narrowing type casting. Widening type casting automatically converts a lower data type to a higher one without data loss. Narrowing type casting manually converts a higher data type to a lower one through parentheses, which can result in data loss.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

JAVA TYPE CASTING

Java Type Casting


• The process of converting the value
of one data type (int, float, double,
etc.) to another data type is known as
typecasting.
Two major types.
1. Widening Type Casting
2. Narrowing Type Casting
Widening Type Casting
• In Widening Type Casting, Java
automatically converts one data to
another data type
class Main {
public static void main(String[] args) {

// create int type variable


int num = 10;
System.out.println("The integer value: " + num);

// convert into double type


double data = num; System.out.println("The double value: " + data);
}
}
class Main {
public static void main(String[] args) {

// create int type variable


int num = 10;
System.out.println("The integer value: " + num);

// convert into double type


double data = num; System.out.println("The double value: " + data);
}
Output:
} The integer value: 10
The double value: 10.0
class Main {
public static void main(String[] args) {

// create int type variable


int num = 10;
System.out.println("The integer value: " + num);

// convert into double type


double data = num; System.out.println("The double value: " + data);
}
Output:
} The integer value: 10
The double value: 10.0
class Main {
public static void main(String[] args) {

// create int type variable


int num = 10;
System.out.println("The integer value: " + num);

// convert into double type


double data = num; System.out.println("The double value: " + data);
}
Output:
} The integer value: 10
The double value: 10.0
Widening Type Casting
• In Widening Type Casting, Java
automatically converts one data to
another data type
In the case of Widening Type Casting, the
lower data type (having smaller size) is
converted into the higher data type (having
larger size). Hence there is no loss in data. This
is why this type of conversion happens
automatically.
Note:
This is also known as Implicit Type Casting.
Narrowing Type Casting
• In Narrowing Type Casting, we manually
convert one data type into another using
the parenthesis.
class Main {
public static void main(String[] args) {
// create double type variable
double num = 10.99; System.out.println("The double value: " +
num);
// convert into int type
int data = (int)num;
System.out.println("The integer value: " + data);
}
}
Output:
The double value: 10.99
The integer value: 10.0
In the case of Narrowing Type Casting, the
higher data types (having larger size) are
converted into lower data types (having smaller
size). Hence there is the loss of data. This is why
this type of conversion does not happen
automatically.
Note:
int data = (int) num.
Here, the int keyword inside the parenthesis indicates that the num variable is converted
into the int type.

You might also like