Core Java: byte and short overflow silently wraps around, no exception

byte and short have no overflow checking. Incrementing past MAX_VALUE silently wraps to the type's minimum instead of throwing.

Code
byte b = 127;
b++;
short s = 32767;
s++;
System.out.println("byte 127 + 1 = " + b);
System.out.println("short 32767 + 1 = " + s);
Output
byte 127 + 1 = -128
short 32767 + 1 = -32768
Advertisement
More in JAVA

Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.

Published 2026-09-27

© Java Coding Hub · About · Contact · Privacy · Terms