01 / 21
int double

Type Casting
in Java

Master widening and narrowing conversions to safely transform data between Java's primitive types.

Java AP Computer Science A

What is Type Casting?

Type casting is the process of converting a value from one data type to another. In Java, every variable has a declared type, and sometimes you need to store a value in a variable of a different type.

Implicit Casting
Java does it automatically when it is safe: no data loss possible.
Explicit Casting
You must write the cast yourself because data loss is possible.

Java's Primitive Type Hierarchy

byte 8 bits short 16 bits char 16 bits int 32 bits long 64 bits float 32 bits double 64 bits Widening direction → (smaller to larger capacity)

Why Does Casting Matter?

Java is a strongly typed language. The compiler will not let you silently lose data. Understanding casting helps you:

1
Avoid compiler errors when assigning a larger type to a smaller variable.
2
Perform integer division correctly: \( 7 / 2 \) is \( 3 \) in Java, but \( (\texttt{double})\, 7 / 2 \) is \( 3.5 \).
3
Prevent unexpected data loss when working with mixed-type expressions.
4
Work with APIs that return a different type than what your variable expects.

Widening Conversion (Implicit)

When you assign a smaller type to a larger type, Java automatically promotes the value. No cast syntax needed.

int score = 95; double average = score; // Widening: int → double System.out.println(average); // 95.0
int 95 auto double 95.0

Narrowing Conversion (Explicit)

When you assign a larger type to a smaller type, you must write an explicit cast. Java truncates the value to fit.

double price = 9.99; int rounded = (int) price; // Narrowing: double → int System.out.println(rounded); // 9 (decimal part lost!)
double 9.99 (int) int 9 .99 lost!

The Cast Syntax

The general form of an explicit cast in Java uses parentheses around the target type, placed before the expression:

// General form: targetType variable = (targetType) expression; // Examples: int x = (int) 3.14; // x = 3 byte b = (byte) 300; // b = 44 (overflow!) int half = (int) (7.0 / 2); // half = 3

Widening vs Narrowing

Widening Narrowing
Direction Smaller → Larger Larger → Smaller
Syntax needed? No (implicit) Yes (explicit cast)
Data loss? None Possible (truncation/overflow)
Compiler error? No Yes, if cast is missing
Example int → double double → int

How to Apply Type Casting

1
Identify both types: What type does the value currently have? What type does the target variable expect?
2
Check the hierarchy: Is the source type smaller or larger than the target? Refer to the widening path.
3
If widening: Simply assign. Java handles it: no cast needed.
4
If narrowing: Add an explicit cast: (targetType) before the expression.
5
Consider data loss: Will truncation or overflow affect your program? Add a comment if the loss is intentional.

Example 1: Integer Division Fix

Problem: You want the precise result of dividing two integers, but Java gives you an integer result.

int a = 7; int b = 2; // Without casting: integer division double result1 = a / b; // 3.0 (not 3.5!) // With casting: force double division double result2 = (double) a / b; // 3.5 ✓

Example 2: Storing a Calculation

Problem: A method returns a double, but you need to store it as an int for an array index.

double rawIndex = 4.7; // This will NOT compile: // int index = rawIndex; // Error! // Fix with explicit cast: int index = (int) rawIndex; // index = 4 (truncated) // If you need rounding instead: int rounded = (int) Math.round(rawIndex); // rounded = 5

Example 3: Mixed-Type Expressions

Problem: What happens when you mix types in an arithmetic expression?

int count = 10; double rate = 0.15; // Java auto-widens count to double before multiplying double result = count * rate; // 1.5 // What if we want an int result? int truncated = (int) (count * rate); // 1

Automatic Promotion Rules

Rule 1 If either operand is double, the other is promoted to double Rule 2 Otherwise, if either is float, the other is promoted to float Rule 3 Otherwise, if either is long, the other is promoted to long Rule 4 Otherwise, both operands are promoted to int (even byte + byte = int)

char and int Casting

In Java, a char is a 16-bit unsigned integer representing a Unicode character. You can cast freely between char and int.

char letter = 'A'; int ascii = letter; // Widening: 65 int code = 97; char ch = (char) code; // Narrowing: 'a' // Arithmetic with chars: char next = (char) (letter + 1); // 'B'

Common Mistakes

✕ Forgetting the Cast
Writing int x = 3.14; without (int) causes a compiler error. Java won't narrow implicitly.
✕ Casting Too Late
Writing (double)(7/2) casts after integer division already happened: result is 3.0, not 3.5.
✕ Expecting Rounding
Casting (int) 9.99 gives 9, not 10. Java truncates toward zero, it does not round.

Summary Reference Table

Scenario Code Result
int → double Implicit Safe, no loss
double → int (int) val Truncated
int / int a / b Integer division
Force double division (double) a / b Precise result
char → int Implicit Unicode value
int → char (char) val Character

Question 1 of 5

What is the output of the following code?
double x = 7 / 2;
System.out.println(x);

Question 2 of 5

Which of the following requires an explicit cast in Java?

Question 3 of 5

What is the value of result after this code executes?
double result = (double) 7 / 2;

Question 4 of 5

What does (int) -3.7 evaluate to in Java?

Question 5 of 5

Given:
byte b = (byte) 130;
What is the value of b?