Night 2-Java Primitive Types: Overflow and Type Casting

Overflow and wrap-around in integer types

  • Primitive integer types: extbyte,extshort,extint,extlongext{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: 2311=21474836472^{31}-1 = 2\,147\,483\,647

  • Smallest int value: 231=2147483648-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):

    • extbyte=1 byteext{byte} = 1\text{ byte}

    • extshort=2 bytesext{short} = 2\text{ bytes}

    • extint=4 bytesext{int} = 4\text{ bytes}

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

  • Floating point types: float=4 bytes\text{float} = 4\text{ bytes}, double=8 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: byteshort,byteint,intfloat,longdouble\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., intfloat\text{int} \rightarrow \text{float}) is allowed automatically.

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

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

    • Syntax: shortb=(short)a;\text{short} b = (short) a;, floatb=(float)a;\text{float} b = (float) a;, longb=(long)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: doublea=10.5;intb=(int)a;\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 f\text{f} or F\text{F}: e.g., 10f,3.14f10f, 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: byteshort,shortint,intdouble,floatdouble.\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: intshort,doublelong,doublefloat.\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): byte,short,int,long,float,double,char,boolean.\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: byteshort,shortint,intfloat,longdouble.\text{byte} \to \text{short}, \text{short} \to \text{int}, \text{int} \to \text{float}, \text{long} \to \text{double}.

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

// End of notes