Skip to main content
๐Ÿ“œ WAYPOINT LESSON

Overflow and mixed types

โญ beginnerโณ 10 min read๐Ÿ“ Lesson 7 of 256

int range, silent overflow, String concatenation order, char arithmetic.

An int has 32 bits: values from โˆ’2ยณยน (โˆ’2147483648) to 2ยณยน โˆ’ 1 (2147483647).

Overflow wraps around silently:

int big = 2147483647;
big = big + 1;
System.out.println(big);    // -2147483648 โ€” wrapped!

Two int-only operators the exam loves:

  • + on ints with Strings: "score: " + 90 โ†’ "score: 90" (concatenation). Left to right: 1 + 2 + "x" โ†’ "3x", but "x" + 1 + 2 โ†’ "x12".
  • % (remainder) works on negatives with the sign of the dividend: -7 % 3 is -1.

char participates in arithmetic through its Unicode value:

char c = 'A';            // 65
char next = (char) (c + 1);   // 'B'

Casting an int that is out of char's range produces a different character โ€” another silent wrap.