[post-views]

In this lesson, you will learn
Type Conversion and Casting in Java are fundamental concepts used to transform data from one type to another.
In Java, there are two types of conversions: implicit and explicit.

int to a long, or a float to a double.
public class ImplicitTypeConversionExample {
public static void main(String[] args) {
// Starting with a byte
byte myByte = 100;
char myChar='A';
// Implicit conversion from byte to short
short myShort = myByte;
// Implicit conversion from short to int
int myInt = myShort;
// Implicit conversion from char to int
int myInt1 = myChar;
// Implicit conversion from int to long
long myLong = myInt;
// Implicit conversion from long to float
float myFloat = myLong;
// Finally, implicit conversion from float to double
double myDouble = myFloat;
// Displaying all the converted values
System.out.println("Byte value: " + myByte);
System.out.println("Short value (from byte): " + myShort);
System.out.println("Int value (from short): " + myInt);
System.out.println("Int value (from char): " + myInt1);
System.out.println("Long value (from int): " + myLong);
System.out.println("Float value (from long): " + myFloat);
System.out.println("Double value (from float): " + myDouble);
}
}
Output
Byte value: 100
Short value (from byte): 100
Int value (from short): 100
Int value (from char): 65
Long value (from int): 100
Float value (from long): 100.0
Double value (from float): 100.0
int a = 5;
double b = 6.2;
// a is automatically converted to double
double result = a + b;
Here, a is an int and b is a double. In the expression a + b, a is automatically converted to double before the addition since double is a wider type than int. The result of the operation is of type double.
double to an int.
The syntax for type casting in Java is:
(targetType) variableToBeCasted;
Here, targetType is the type you are casting to, and variableToBeCasted is the variable you want to cast.
Below is an example that demonstrates explicit casting among different types in Java, from larger to smaller types, covering double, float, long, int, short, and byte.
public class TypeCasting{
public static void main(String[] args) {
// Starting with a double
double myDouble = 129.99;
// Casting double to float
float myFloat = (float) myDouble;
// Casting float to long
long myLong = (long) myFloat;
// Casting long to int
int myInt = (int) myLong;
// Casting int to short
short myShort = (short) myInt;
// Finally, casting short to byte
byte myByte = (byte) myShort;
// Displaying all the converted values
System.out.println("Double value: " + myDouble);
System.out.println("Float value (from double): " + myFloat);
System.out.println("Long value (from float): " + myLong);
System.out.println("Int value (from long): " + myInt);
System.out.println("Short value (from int): " + myShort);
System.out.println("Byte value (from short): " + myByte);
//Outputs -127, overflow occurs due to byte range (-128 to 127)
}
}
Output
Double value: 129.99
Float value (from double): 129.99
Long value (from float): 129
Int value (from long): 129
Short value (from int): 129
Byte value (from short): -127
char myChar = 'A'; // 'A' has an ASCII value of 65
byte myByte = (byte) myChar; // Explicit casting is required
System.out.println("Original char value: " + myChar);
// Outputs A
System.out.println("Converted byte value: " + myByte);
// Outputs 65, ASCII value of 'A'
1. Data Loss: When performing narrowing casting, there is a risk of data loss. For example, casting a double to a int might lose the fractional part.
2. Precision Loss: Even when casting between numeric types, precision can be lost (e.g., float to int).
3. ClassCastException: In object casting, a ClassCastException can occur if you try to cast an object to a subclass of which it is not an instance.
Good
The content is detailed and awesome. The topic wise flow is good. Never loose interest while reading it.
good
You must be logged in to submit a review.