Night 2-Java Primitive Types: Overflow and Type Casting

Overflow and wrap-around in integer types

  • Primitive integer types: ext{byte}, ext{short}, ext{int}, ext{long} (signed, increasing in size).

  • Integers store only whole numbers; they cannot store fractions.

  • Exceeding the maximum or going below the minimum causes wrap-around (no crash, just roll over).

  • Largest int value: 2^{31}-1 = 2\,147\,483\,647

  • Smallest int value: -2^{31} = -2\,147\,483\,648

  • Example behavior: max + 1 wraps to min; min - 1 wraps to max.

  • Analogy: alarm clock/tumbler behavior where rolling past the end resets to the opposite extreme.

Type ranges and memory footprint

  • Integer types store integers only; floating point types store fractions as well.

  • Memory sizes (typical):

    • ext{byte} = 1\text{ byte}

    • ext{short} = 2\text{ bytes}

    • ext{int} = 4\text{ bytes}

    • \text{long} = 8\text{ bytes}

  • Floating point types: \text{float} = 4\text{ bytes}, \text{double} = 8\text{ bytes}

  • When converting between types, remember: integers cannot store fractions; floating point can.

Automatic (implicit) widening vs explicit narrowing (typecasting)

  • Widening (automatic) conversions occur when the destination type is wider (more capable) than the source:

    • Examples: \text{byte} \rightarrow \text{short}, \text{byte} \rightarrow \text{int}, \text{int} \rightarrow \text{float}, \text{long} \rightarrow \text{double}

    • Also: integer to floating (e.g., \text{int} \rightarrow \text{float}) is allowed automatically.

  • Narrowing (explicit) conversions require a cast because data may be lost:

    • Examples that require explicit casts: \text{int} \rightarrow \text{short}, \text{double} \rightarrow \text{float} (if you want to force it), \text{double} \rightarrow \text{long}

    • Syntax: \text{short} b = (short) a;, \text{float} b = (float) a;, \text{long} b = (long) a;

  • The cast affects the value during the assignment; the source variable’s type does not change.

Mixing integer and floating point types

  • Integer types can be copied into floating point types without issue (less capability loss concerns since floating can store fractions).

  • Floating point types copied into integer types are not automatic (loss of fractional part), so they require explicit casts:

    • Example: \text{double} a = 10.5; \text{int} b = (int) a;

  • If you try to copy a floating point value into an integer without a cast, you’ll get an error.

Literals and numeric constants in Java

  • Decimal literals with a fractional part are treated as the largest floating type by default (double).

  • To force a float literal, suffix with \text{f} or \text{F}: e.g., 10f, 3.14f .

  • When assigning a float literal to a float variable, use the suffix to avoid the default double.

Practical takeaways (quick rules)

  • Widening conversions are automatic: destination is wider/more capable.

    • Examples: \text{byte} \rightarrow \text{short}, \text{short} \rightarrow \text{int}, \text{int} \rightarrow \text{double}, \text{float} \rightarrow \text{double}.

  • Narrowing conversions require explicit casts: destination is narrower or loses capability.

    • Examples: \text{int} \rightarrow \text{short}, \text{double} \rightarrow \text{long}, \text{double} \rightarrow \text{float}.

  • Mixed conversions: integer -> floating is fine; floating -> integer requires a cast.

  • The cast does not mutate the source variable; it affects the value as it's assigned to the target.

Quick reference cheat sheet

  • Eight primitive types (overview): \text{byte}, \text{short}, \text{int}, \text{long}, \text{float}, \text{double}, \text{char}, \text{boolean}.

  • Overflow behavior for integers: wrap-around from max to min and vice versa.

  • Automatic widening examples: \text{byte} \to \text{short}, \text{short} \to \text{int}, \text{int} \to \text{float}, \text{long} \to \text{double}.

  • Forced narrowing examples: \text{short} = (\text{short}) \text{intVal};, \text{float} = (\text{float}) \text{doubleVal};

// End of notes