π±BeginnerVariables & Data Types
Type Casting
Learn how to convert between numeric types in Java using widening (automatic) and narrowing (explicit) type casting.
Overview
Type casting is converting a value from one data type to another. Java has two kinds: widening conversion (automatic, safe) and narrowing conversion (manual, may lose data).
Widening happens automatically when you assign a smaller type to a larger type - for example, assigning an int to a double. Narrowing requires an explicit cast using (type) syntax because Java does not do it automatically, since data loss could occur (e.g., 3.99 cast to int becomes 3).
Key Points
- β’Widening: smaller β larger type is automatic. byte β short β int β long β float β double
- β’Narrowing: larger β smaller type requires explicit cast `(targetType)value`
- β’Narrowing truncates (not rounds): (int) 3.99 = 3, not 4
- β’Casting between int and char: (char) 65 = 'A', (int) 'A' = 65
- β’String to int: Integer.parseInt("42"); String to double: Double.parseDouble("3.14")
- β’int/long to String: String.valueOf(42) or "" + 42
Examples
Widening - automatic, no cast needed
public class Main {
public static void main(String[] args) {
int myInt = 100;
long myLong = myInt; // int β long: automatic
double myDouble = myInt; // int β double: automatic
System.out.println(myInt); // 100
System.out.println(myLong); // 100
System.out.println(myDouble); // 100.0
}
}No cast syntax needed for widening. Java widens automatically because no information is lost.
Narrowing - explicit cast required
public class Main {
public static void main(String[] args) {
double price = 9.99;
int wholePart = (int) price; // explicit cast: truncates decimal
System.out.println(wholePart); // 9 (not 10!)
long bigNumber = 1_000_000L;
int smaller = (int) bigNumber; // may lose data if > Integer.MAX_VALUE
System.out.println(smaller); // 1000000
// char β int
char c = 'A';
int ascii = (int) c;
System.out.println(ascii); // 65
System.out.println((char)(ascii + 1)); // B
}
}Narrowing truncates, it does not round. (int) 9.99 = 9, (int) 9.99 β 10.
Parsing Strings to numbers
public class Main {
public static void main(String[] args) {
String numStr = "42";
String decStr = "3.14";
int i = Integer.parseInt(numStr);
double d = Double.parseDouble(decStr);
System.out.println(i + 8); // 50
System.out.println(d * 2); // 6.28
// Number to String
String s = String.valueOf(i);
System.out.println(s + "!"); // 42!
}
}parseInt and parseDouble throw NumberFormatException if the String is not a valid number.
Common Mistakes
- Expecting (int) 9.99 to round to 10 - Java truncates toward zero, so you get 9
- Passing a non-numeric String to Integer.parseInt: `Integer.parseInt("abc")` throws an exception
- Losing data silently with narrowing: (byte) 200 wraps around to -56 because 200 exceeds byte's range
Note
Use Math.round() if you want rounding rather than truncation: Math.round(9.99) returns 10L.
Exercises
Write Java code and click Run to execute it instantly. When your output looks correct, mark as done.
Loading editorβ¦
Completed: 0 / 4 exercises
Knowledge Check
Question 1 of 4