Type casting helps you change a value from one type to another so your program can work with different kinds of data easily.
Type casting in Java
targetType variable = (targetType) value;
The targetType is the type you want to convert to.
The value after the cast operator is the original value you want to cast.
int x = (int) 3.14;
double y = (double) 5;
byte b = (byte) 130;
This program shows how to cast a double to an int and an int to a double. It prints the values before and after casting.
public class Main { public static void main(String[] args) { double pi = 3.99; int wholeNumber = (int) pi; System.out.println("Double value: " + pi); System.out.println("After casting to int: " + wholeNumber); int number = 100; double decimalNumber = (double) number; System.out.println("Int value: " + number); System.out.println("After casting to double: " + decimalNumber); } }
Casting from a bigger type to a smaller type can lose data or cause unexpected results.
Automatic casting (called widening) happens when going from smaller to bigger types, like int to double.
Explicit casting (called narrowing) is needed when going from bigger to smaller types, like double to int.
Type casting changes a value from one type to another.
Use explicit casting when converting from bigger to smaller types.
Be careful of data loss when casting to smaller types.
Practice
What is type casting in Java?
Solution
Step 1: Understand the meaning of type casting
Type casting means converting a value from one data type to another, like from int to double or vice versa.Step 2: Identify the correct description
Among the options, only changing a value's data type matches the definition of type casting.Final Answer:
Changing a value from one data type to another -> Option DQuick Check:
Type casting = Changing data type [OK]
- Confusing type casting with variable declaration
- Thinking type casting creates new variables
- Mixing type casting with control structures
Which of the following is the correct syntax for explicit type casting from double to int in Java?
Solution
Step 1: Recall Java explicit casting syntax
In Java, explicit casting uses parentheses with the target type before the value, like (int) value.Step 2: Match syntax with options
Only int x = (int) 3.14; uses the correct syntax:int x = (int) 3.14;. Others are invalid in Java.Final Answer:
int x = (int) 3.14; -> Option AQuick Check:
Explicit cast uses (type) value [OK]
- Using function-like cast syntax (e.g., int(3.14))
- Using 'as' keyword which Java doesn't support
- Writing 'cast' keyword which is invalid
What is the output of the following Java code?
double d = 9.99; int i = (int) d; System.out.println(i);
Solution
Step 1: Understand explicit cast from double to int
Casting a double to int truncates the decimal part, it does not round.Step 2: Apply casting to value 9.99
When casting 9.99 to int, the decimal .99 is dropped, resulting in 9.Final Answer:
9 -> Option CQuick Check:
Double to int cast truncates decimals = 9 [OK]
- Assuming casting rounds the number
- Expecting compilation error due to cast
- Confusing output with original double value
Identify the error in the following Java code snippet:
byte b = 130;
Solution
Step 1: Understand byte range in Java
Byte type can hold values from -128 to 127 only.Step 2: Check assigned value 130
130 is outside byte range, so Java reports a compile-time error about possible lossy conversion.Final Answer:
Type mismatch: possible lossy conversion from int to byte -> Option AQuick Check:
Byte range exceeded causes compile error [OK]
- Thinking code runs without error
- Expecting runtime exception instead of compile error
- Missing need for explicit cast
Given the code below, what will be the output?
int a = 300; byte b = (byte) a; System.out.println(b);
Note: byte range is -128 to 127.
Solution
Step 1: Understand casting int 300 to byte
When casting int to byte, Java keeps only the lowest 8 bits of the int value.Step 2: Calculate byte value from 300
300 in binary is 00000001 00101100 (16 bits). The lowest 8 bits are 00101100 (decimal 44).Step 3: Confirm signed byte value
300 mod 256 = 44. However, since byte is signed, the value wraps around to -56.Final Answer:
-56 -> Option BQuick Check:
Int to byte cast wraps around signed byte range = -56 [OK]
- Assuming output is original int value
- Confusing signed byte range with unsigned
- Expecting compilation error without cast
Ready to prove it?
Pass the test and this pattern moves unknown
