Skip to content
Output prediction quizbeginner · 2 min read · 2026-11-13

What does this Java cast print? 🤔 | Type casting

This Short explains primitive numeric conversions: widening goes to a larger numeric type and can be implicit, while narrowing goes to a smaller/riskier type and normally needs an explicit cast. When an integer is narrowed, Java keeps only the low-order bits, so a cast can produce an overflow-like surprising value, such as int 130 becoming byte -126.

This lesson goes with our YouTube Short. Watch it, then work through the example and the practice below.

Subscribe on YouTube

Open on YouTube

What you will learn

  • Widening example: int to long is allowed implicitly: long x = intValue;
  • Narrowing example: int to byte needs an explicit cast: byte b = (byte) intValue;
  • Narrowing may lose range and magnitude; it is not a safety check.
  • For int to byte, Java keeps only the lowest 8 bits, so 130 becomes -126.

The idea

This Short explains primitive numeric conversions: widening goes to a larger numeric type and can be implicit, while narrowing goes to a smaller/riskier type and normally needs an explicit cast. When an integer is narrowed, Java keeps only the low-order bits, so a cast can produce an overflow-like surprising value, such as int 130 becoming byte -126.

Key points

  • Widening example: int to long is allowed implicitly: long x = intValue;
  • Narrowing example: int to byte needs an explicit cast: byte b = (byte) intValue;
  • Narrowing may lose range and magnitude; it is not a safety check.
  • For int to byte, Java keeps only the lowest 8 bits, so 130 becomes -126.
  • Primitive narrowing overflow/underflow does not throw a runtime exception.

About the example

Java 21 demo: implicit widening int→long, explicit narrowing int→byte, and overflow-like result after cast.

Code

Java · Main.java✓ Executed & verified · Java 21.0.13
public class Main {
  public static void main(String[] args) {
    int marks = 130;
    long safe = marks;
    byte risky = (byte) marks;

    System.out.println("int: " + marks);
    System.out.println("long: " + safe);
    System.out.println("byte: " + risky);
  }
}
Output (real run)
int: 130
long: 130
byte: -126

This program was compiled and executed with Java 21.0.13 before the video was made.

Common mistakes

  • ✕ Do not assume a cast validates the value; it just forces the conversion.

    ✓ See the example program and the key points above.

  • ✕ Do not expect an exception when int is cast to byte and overflows; Java still produces a byte value.

    ✓ See the example program and the key points above.

  • ✕ For constants, Java has special assignment rules, so keep the Short focused on variable casting to avoid confusion.

    ✓ See the example program and the key points above.

Practice

  1. Change the example so it prints something different, then predict the new output before running it.

Key takeaways

  • Widening example: int to long is allowed implicitly: long x = intValue;
  • Narrowing example: int to byte needs an explicit cast: byte b = (byte) intValue;
  • Narrowing may lose range and magnitude; it is not a safety check.
  • For int to byte, Java keeps only the lowest 8 bits, so 130 becomes -126.
  • Primitive narrowing overflow/underflow does not throw a runtime exception.

Related YouTube lessons

See the full Java learning path

Keep learning