☕ Java Type Casting
Understanding Automatic & Manual Type Conversions
🔄Widening Casting (Automatic)
Smaller Type → Larger Type (Safe & Automatic)
byte
(8 bits)
(8 bits)
→
short
(16 bits)
(16 bits)
→
int
(32 bits)
(32 bits)
→
long
(64 bits)
(64 bits)
→
float
(32 bits)
(32 bits)
→
double
(64 bits)
(64 bits)
⚠️Narrowing Casting (Manual)
Larger Type → Smaller Type (Requires Manual Casting)
double
(64 bits)
(64 bits)
←
float
(32 bits)
(32 bits)
←
long
(64 bits)
(64 bits)
←
int
(32 bits)
(32 bits)
←
short
(16 bits)
(16 bits)
←
byte
(8 bits)
(8 bits)
💻Detailed Code Examples
🔄Widening Casting Examples
Example 1: Complete Program - byte → int
public class WideningExample1 {
public static void main(String[] args) {
// Declare and initialize a byte variable
byte smallNumber = 100;
// Automatic widening casting: byte to int
int largerNumber = smallNumber;
// Display original and converted values
System.out.println("=== Widening Casting: byte to int ===");
System.out.println("Original byte value: " + smallNumber);
System.out.println("Converted int value: " + largerNumber);
System.out.println("Data loss: None (Safe conversion)");
}
}
public static void main(String[] args) {
// Declare and initialize a byte variable
byte smallNumber = 100;
// Automatic widening casting: byte to int
int largerNumber = smallNumber;
// Display original and converted values
System.out.println("=== Widening Casting: byte to int ===");
System.out.println("Original byte value: " + smallNumber);
System.out.println("Converted int value: " + largerNumber);
System.out.println("Data loss: None (Safe conversion)");
}
}
📤 Output:
=== Widening Casting: byte to int ===
Original byte value: 100
Converted int value: 100
Data loss: None (Safe conversion)
💡 Explanation: The byte value 100 is automatically converted to int without any data loss. No explicit casting needed! Java handles this conversion seamlessly.
=== Widening Casting: byte to int ===
Original byte value: 100
Converted int value: 100
Data loss: None (Safe conversion)
💡 Explanation: The byte value 100 is automatically converted to int without any data loss. No explicit casting needed! Java handles this conversion seamlessly.
Example 2: Complete Program - int → double
public class WideningExample2 {
public static void main(String[] args) {
// Declare and initialize an int variable
int wholeNumber = 42;
// Automatic widening casting: int to double
double decimalNumber = wholeNumber;
// Display original and converted values
System.out.println("=== Widening Casting: int to double ===");
System.out.println("Original int value: " + wholeNumber);
System.out.println("Converted double value: " + decimalNumber);
System.out.println("Notice: Decimal point added automatically");
}
}
public static void main(String[] args) {
// Declare and initialize an int variable
int wholeNumber = 42;
// Automatic widening casting: int to double
double decimalNumber = wholeNumber;
// Display original and converted values
System.out.println("=== Widening Casting: int to double ===");
System.out.println("Original int value: " + wholeNumber);
System.out.println("Converted double value: " + decimalNumber);
System.out.println("Notice: Decimal point added automatically");
}
}
📤 Output:
=== Widening Casting: int to double ===
Original int value: 42
Converted double value: 42.0
Notice: Decimal point added automatically
💡 Explanation: The integer 42 becomes 42.0 when converted to double. Notice the decimal point is added automatically to represent the floating-point format.
=== Widening Casting: int to double ===
Original int value: 42
Converted double value: 42.0
Notice: Decimal point added automatically
💡 Explanation: The integer 42 becomes 42.0 when converted to double. Notice the decimal point is added automatically to represent the floating-point format.
⚠️Narrowing Casting Examples
Example 1: Complete Program - double → int (Data Loss!)
public class NarrowingExample1 {
public static void main(String[] args) {
// Declare and initialize a double variable
double preciseNumber = 15.87;
// Manual narrowing casting: double to int
int roundedNumber = (int) preciseNumber;
// Display original and converted values
System.out.println("=== Narrowing Casting: double to int ===");
System.out.println("Original double value: " + preciseNumber);
System.out.println("Converted int value: " + roundedNumber);
System.out.println("Data lost: " + (preciseNumber - roundedNumber));
}
}
public static void main(String[] args) {
// Declare and initialize a double variable
double preciseNumber = 15.87;
// Manual narrowing casting: double to int
int roundedNumber = (int) preciseNumber;
// Display original and converted values
System.out.println("=== Narrowing Casting: double to int ===");
System.out.println("Original double value: " + preciseNumber);
System.out.println("Converted int value: " + roundedNumber);
System.out.println("Data lost: " + (preciseNumber - roundedNumber));
}
}
📤 Output:
=== Narrowing Casting: double to int ===
Original double value: 15.87
Converted int value: 15
Data lost: 0.8700000000000001
⚠️ Explanation: The decimal part (.87) is completely lost! Java truncates (cuts off) the decimal portion, it doesn't round. Notice the small floating-point precision error in the calculation.
=== Narrowing Casting: double to int ===
Original double value: 15.87
Converted int value: 15
Data lost: 0.8700000000000001
⚠️ Explanation: The decimal part (.87) is completely lost! Java truncates (cuts off) the decimal portion, it doesn't round. Notice the small floating-point precision error in the calculation.
Example 2: Complete Program - long → short (Potential Overflow!)
public class NarrowingExample2 {
public static void main(String[] args) {
// Declare and initialize a long variable
long bigNumber = 50000L;
// Manual narrowing casting: long to short
short smallNumber = (short) bigNumber;
// Display original and converted values
System.out.println("=== Narrowing Casting: long to short ===");
System.out.println("Original long value: " + bigNumber);
System.out.println("Converted short value: " + smallNumber);
System.out.println("Short range: -32,768 to 32,767");
System.out.println("Overflow occurred: " + (bigNumber > 32767));
}
}
public static void main(String[] args) {
// Declare and initialize a long variable
long bigNumber = 50000L;
// Manual narrowing casting: long to short
short smallNumber = (short) bigNumber;
// Display original and converted values
System.out.println("=== Narrowing Casting: long to short ===");
System.out.println("Original long value: " + bigNumber);
System.out.println("Converted short value: " + smallNumber);
System.out.println("Short range: -32,768 to 32,767");
System.out.println("Overflow occurred: " + (bigNumber > 32767));
}
}
📤 Output:
=== Narrowing Casting: long to short ===
Original long value: 50000
Converted short value: -15536
Short range: -32,768 to 32,767
Overflow occurred: true
🚨 Explanation: Overflow occurred! 50000 is too large for short (max: 32767). The result wraps around and becomes negative due to two's complement representation.
=== Narrowing Casting: long to short ===
Original long value: 50000
Converted short value: -15536
Short range: -32,768 to 32,767
Overflow occurred: true
🚨 Explanation: Overflow occurred! 50000 is too large for short (max: 32767). The result wraps around and becomes negative due to two's complement representation.
Example 3: Safe Narrowing (Within Range)
double safeNumber = 25.0;
int convertedNumber = (int) safeNumber; // Manual casting required
System.out.println("Original double: " + safeNumber);
System.out.println("Converted int: " + convertedNumber);
int convertedNumber = (int) safeNumber; // Manual casting required
System.out.println("Original double: " + safeNumber);
System.out.println("Converted int: " + convertedNumber);
📤 Output:
Original double: 25.0
Converted int: 25
✅ Explanation: Even though manual casting is required, no data is lost here because 25.0 fits perfectly in an int and has no fractional part.
Original double: 25.0
Converted int: 25
✅ Explanation: Even though manual casting is required, no data is lost here because 25.0 fits perfectly in an int and has no fractional part.


If you have any doubts, Please let me know