Bird
Raised Fist0
Javaprogramming~10 mins

Type promotion in Java - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Type promotion
Declare smaller type variable
↓
Use in expression with larger type
↓
Java promotes smaller type to larger type
↓
Expression evaluated as larger type
↓
Assign result to compatible type variable
Java automatically converts smaller data types to larger ones in expressions to avoid data loss.
Execution Sample
Java
byte b = 10;
int i = b + 5;
System.out.println(i);
Adds a byte and an int; byte is promoted to int before addition.
Execution Table
StepExpressionType BeforeType After PromotionValueAction
1bbytebyte10Variable declared
25intint5Literal int
3b + 5byte + intint + int15b promoted to int, addition done
4i = b + 5intint15Assign result to int variable
5System.out.println(i)intint15Print value
💡 Execution ends after printing the promoted and calculated int value 15.
Variable Tracker
VariableStartAfter ExpressionFinal
b10 (byte)10 (byte)10 (byte)
iuninitialized15 (int)15 (int)
Key Moments - 2 Insights
Why does Java promote 'byte' to 'int' in the expression 'b + 5'?
Java promotes smaller types like byte to int in arithmetic expressions to avoid data loss and to use a common type for calculation, as shown in execution_table row 3.
Can we assign the result of 'b + 5' directly back to a byte variable without casting?
No, because after promotion the result is int, which is larger than byte. Assigning int to byte without cast causes error, as shown in execution_table row 4 where 'i' is int.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the type of 'b + 5' after promotion?
Aint
Bbyte
Cshort
Dlong
💡 Hint
Check the 'Type After Promotion' column in execution_table row 3.
At which step is the value 15 assigned to variable 'i'?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'Action' column for assignment in execution_table.
If 'b' was declared as int instead of byte, how would the promotion step change?
APromoted to long
BNo promotion needed, both are int
CPromoted to byte
DPromoted to double
💡 Hint
Refer to variable_tracker and execution_table type columns for type changes.
Concept Snapshot
Type promotion in Java:
- Smaller types (byte, short, char) promoted to int in expressions
- Ensures safe arithmetic without data loss
- Result type is at least int
- Assigning back to smaller type needs explicit cast
- Common in arithmetic and mixed-type expressions
Full Transcript
This example shows how Java promotes smaller types like byte to int when used in expressions. The variable 'b' is a byte with value 10. When added to the int literal 5, Java promotes 'b' to int before addition. The result is an int value 15, which is assigned to the int variable 'i'. Finally, printing 'i' outputs 15. This automatic type promotion prevents data loss during calculations and is a key behavior in Java arithmetic.

Practice

(1/5)
1. Which of the following statements about Java type promotion is correct?
easy
A. long is always promoted to byte in expressions.
B. byte, short, and char are promoted to int before arithmetic operations.
C. double is promoted to float when mixed with float.
D. int is promoted to byte automatically in expressions.

Solution

  1. Step 1: Understand type promotion basics

    Java promotes smaller integer types (byte, short, char) to int before arithmetic to avoid data loss.
  2. Step 2: Evaluate each option

    byte, short, and char are promoted to int before arithmetic operations. correctly states this rule. Options A, B, and C are incorrect because long is not promoted to byte, int is not promoted to byte, and double is not promoted to float.
  3. Final Answer:

    byte, short, and char are promoted to int before arithmetic operations. -> Option B
  4. Quick Check:

    byte/short/char -> int promotion = D [OK]
Hint: Remember: small integers become int before math [OK]
Common Mistakes:
  • Thinking int is promoted to byte
  • Confusing float and double promotion
  • Assuming long is promoted to smaller types
2. Which of the following Java code snippets correctly compiles without explicit casting due to type promotion?
easy
A. int result = (byte)10 + (short)20;
B. short s = 10 + 20;
C. byte b = 10 + 20;
D. int sum = 10 + 20;

Solution

  1. Step 1: Analyze type promotion in arithmetic

    In expressions, byte and short are promoted to int, so 10 + 20 is int.
  2. Step 2: Check each assignment

    int result = (byte)10 + (short)20; casts explicitly, so compiles but casting is manual. byte b = 10 + 20; tries to assign int result to byte without cast - error. int sum = 10 + 20; assigns int sum correctly. short s = 10 + 20; tries to assign int to short without cast - error.
  3. Final Answer:

    int sum = 10 + 20; -> Option D
  4. Quick Check:

    byte/short -> int, assign to int = B [OK]
Hint: Assign promoted int results to int variables [OK]
Common Mistakes:
  • Assigning int expression to byte or short without cast
  • Forgetting byte/short promote to int
  • Assuming automatic narrowing conversion
3. What is the output of the following Java code?
byte b = 10;
short s = 20;
char c = 30;
int result = b + s + c;
System.out.println(result);
medium
A. 60.0
B. Syntax error
C. 60
D. Runtime error

Solution

  1. Step 1: Understand type promotion in expression

    byte, short, and char are promoted to int before addition, so b + s + c is int.
  2. Step 2: Calculate the sum

    10 + 20 + 30 = 60, assigned to int variable result, then printed.
  3. Final Answer:

    60 -> Option C
  4. Quick Check:

    byte+short+char -> int sum = 60 [OK]
Hint: Sum small types promoted to int, print int result [OK]
Common Mistakes:
  • Expecting byte or short result type
  • Confusing output with float/double
  • Thinking code causes error
4. Identify the error in the following Java code and fix it:
byte b = 5;
byte c = 10;
byte sum = b + c;
System.out.println(sum);
medium
A. Add explicit cast: byte sum = (byte)(b + c);
B. Change sum to int: int sum = b + c;
C. Change b and c to int: int b = 5, c = 10;
D. No error, code compiles fine.

Solution

  1. Step 1: Understand type promotion in addition

    byte variables b and c are promoted to int during addition, so b + c is int.
  2. Step 2: Fix assignment error

    Assigning int result to byte sum causes error. Adding explicit cast (byte)(b + c) fixes it.
  3. Final Answer:

    Add explicit cast: byte sum = (byte)(b + c); -> Option A
  4. Quick Check:

    byte+byte -> int, cast needed for byte = A [OK]
Hint: Cast int result back to byte to fix assignment error [OK]
Common Mistakes:
  • Ignoring need for cast when assigning to byte
  • Assuming automatic narrowing conversion
  • Changing variable types unnecessarily
5. Consider the following code snippet:
float f = 5.5f;
double d = 10.5;
var result = f + d;
System.out.println(((Object)result).getClass().getSimpleName());

What will be printed and why?
hard
A. double because float is promoted to double in mixed expressions
B. float because float is bigger than double
C. float because var infers float type
D. Compilation error due to var usage

Solution

  1. Step 1: Understand promotion between float and double

    In expressions mixing float and double, float is promoted to double.
  2. Step 2: Determine result type and output

    Variable result is inferred as double by var. Printing class name outputs "Double".
  3. Final Answer:

    double because float is promoted to double in mixed expressions -> Option A
  4. Quick Check:

    float + double -> double result = A [OK]
Hint: float + double results in double type [OK]
Common Mistakes:
  • Assuming var infers float
  • Thinking float is bigger than double
  • Expecting compilation error with var
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard