• byte:
    • Size: 8 bits (1 byte)
    • Range: to ()
    • Use case: Low-level binary data handling, reading raw streams, or saving memory when working with huge arrays of small numbers.
  • short:
    • Size: 16 bits (2 bytes)
    • Range: to ()
    • Use case: Rarely used in modern code, occasionally for legacy data formats.
  • char:
    • Size: 16 bits (2 bytes)
    • Range: 0 to 65,535 (Unsigned)
    • Use case: Represents a single UTF-16 character (e.g., 'a'). Under the hood, it is actually a numeric integer type, which is why you can do arithmetic on characters like char c = 'a' + 1;.
  • boolean:
    • Size: JVM-dependent (typically 1 byte or 4 bytes in arrays).
    • Range: true or false.
    • Note: Not a numeric primitive, but the final primitive type in Java.

1. Why does 2 ^ 32 output 34?

In Java (and most programming languages like C, C++, and Python), the caret symbol ^ is not an exponentiation operator. It is the bitwise XOR (Exclusive OR) operator. When you write 2 ^ 32, Java evaluates the bitwise XOR of the numbers 2 and 32:

  • 2 in binary: 00000000 00000000 00000000 00000010
  • 32 in binary: 00000000 00000000 00000000 00100000 Because none of the bits overlap, XORing them results in adding their values together: 32 + 2 = 34.

How to do exponents in Java

  • Floating-point exponentiation: Math.pow(2, 32) (returns a double).

  • Bitwise shifting (for integers/longs): 1L << 32 (efficiently shifts the bits left by 32 positions).

2. How Min and Max Values Are Calculated

The minimum and maximum limits for Java primitives depend on how they are stored in binary memory.

A. Integer Types (int and long)

int uses 32 bits (4 bytes), and long uses 64 bits (8 bytes). Java uses Two’s Complement representation to store signed integers (both positive and negative numbers).

  • The Sign Bit: The leftmost (most significant) bit is reserved for the sign: 0 for positive, 1 for negative.
  • Remaining Bits for Magnitude:
    • For an int, you have bits left for data ().
    • For a long, you have bits left for data (). Calculations:
  • Integer.MAX_VALUE ( or 2,147,483,647): The largest positive number you can make using bits (since one bit is reserved for the 0 sign). We subtract because zero (0) takes up one of the positive bit patterns.
  • Integer.MIN_VALUE ( or -2,147,483,648): The most negative number. Two’s complement allows one extra negative value because the bit pattern for -0 is repurposed to represent -2,147,483,648.
  • Long.MAX_VALUE (): Maximum value for a 64-bit signed integer.
  • Long.MIN_VALUE (): Minimum value for a 64-bit signed integer.

B. Floating-Point Types (double)

Floating-point numbers (float and double) do not use simple two’s complement. They follow the IEEE 754 standard, which splits the bits into three components: a sign bit, an exponent, and a fraction (mantissa). Because of this complex scientific notation structure:

  • Double.MAX_VALUE (): Represents the largest finite positive number the 64-bit double structure can hold before overflowing to Infinity.
  • Double.MIN_VALUE (): Surprise! Unlike integer types, Double.MIN_VALUE is not negative. It represents the closest positive value greater than zero that a double can represent (denormalized minimum). The maximum negative number is simply -Double.MAX_VALUE.

Floating Point

Decimal to Binary

Add the fraction to itself and retain the integer until the entire sequence becomes 0

Normalization

Normalization is a technique used in computer architecture to standardize the representation of floating-point numbers.

There are two main types :

  • Explicit

    • Move the radix point (dot) to the LHS of MSB.
  • Implicit

    • Move the radix point (dot) to the RHS of MSB.
    • Better one wrt precision