SLIDE 1 / 5

1. The 8 Primitive Types in Memory

Java does not use objects for everything. Primitives are raw mathematical values held directly on the execution stack without pointer overhead. They divide into integers, floating point, character, and boolean.

Signed Integers byte 8 bits short 16 bits int (estándar) 32 bits long (sufijo L) 64 bits Two's complement · Exact numbers Floating, Char, Boolean float (F) 32 bits double 64 bits char (UTF-16 sin signo) 16 bits boolean (true / false) 1 bit (JVM flag) IEEE 754 & Unicode estándar
byte (8 bits) Memory specification
Allowed Range: -128 hasta 127
Default value: 0
Sample code: byte edad = 25;

Ideal for raw network I/O buffers or audio streaming where memory conservation is paramount.

2. Type Casting: Widening vs Narrowing

When assigning a small type into a larger one (Widening), Java converts it automatically. But forcing a larger value into a smaller bucket (Narrowing) silently chops off higher bits, causing integer overflow.

int original = 130; (32 bits en memoria) 00000000 00000000 00000000 10000010 ← Discarded higher 24 bits byte res = (byte) 130; 10000010 Result = -126 !
Overflow Simulator (Casting to byte)
Valor int: 130
Valor byte resultante: -126
Bits conservados (8): 10000010

The MSB is 1, encoding a negative two's complement integer (-128 + 2 = -126).

3. The Integer Division Trap and Modulo (%)

Operating between integers always results in an integer. 5 / 2 produces 2, not 2.5: the fraction is truncated. The modulo operator (%) captures the integer remainder.

5 / 2 (Entero / Entero) 2 Fractional .5 is dropped 5 % 2 = 1 (resto) 5.0 / 2 (Double / Entero) 2.5 Integer 2 is widened to 2.0 double prom = (double) 5 / 2;
Operation Tester
Java Expression: int resultado = 5 / 2;
Inferred Type: int
Stored Value: 2

Beware of averages! Summing 3 integers and dividing by 3 loses precision unless dividing by 3.0.

4. Short-Circuit Evaluation (&& and ||)

The && and || operators never evaluate their right operand if the left operand already decides the outcome. This shields against NullPointerException and zero division.

user != null FALSE (Abierto) user.getEdad() >= 18 [SKIPPED] Saved from NPE! OFF Result: false
Circuit Switcher
Condition: user != null && user.getEdad() >= 18
Step 1 (user != null): false
Step 2 (getEdad()): SKIPPED (Short-circuit)

Because the left operand is false in &&, Java stops immediately. Using single & forces evaluation of the right operand, triggering NullPointerException.

5. Immutability and Constants with final

The final keyword locks a variable against reassignment once initialized. For universal constants, combine it with static and use UPPER_SNAKE_CASE naming.

final double PI = 3.14159; IMMUTABLE VALUE PI = 3.14; javac Error: cannot assign a value to final

Java Type Best Practices

  • Elegir int por defecto: int is optimal for CPU registers; use long only for large IDs or timestamps.
  • Cuidado con Narrowing: Narrowing casts silently chop off MSB bits causing unexpected negative numbers.
  • Divisiones exactas: To keep decimals, ensure at least one operand is cast to double or has .0.
  • Siempre && y ||: Always use short-circuit && and || to avoid NullPointerException.