ReviseAlgo Logo

Basic Syntax

Data Types

Deep dive into primitive types (sizes, defaults, ranges), memory representation (stack vs heap), and reference types.

Interview: Heavily tested on overflow/underflow, floating-point precision issues, and primitive vs reference comparison.

Last Updated: June 13, 2026 12 min read

Java is a statically-typed language, meaning every variable must be declared with a data type before compilation. Java partitions types into two distinct systems: Primitive Types (predefined by the compiler, representing raw values) and Reference Types (representing dynamically allocated objects on the heap).

Core Idea

Primitive types store raw value bits directly in stack memory frames. Reference types hold the memory addresses of objects that sit on the Heap.

Why It Matters

Using the wrong data type causes numerical overflows, precision drift (e.g. using float for cash calculations), or heavy memory overhead (due to box allocations).

Interview Lens

Focuses on range limits (byte overflow), differences from C++ data types, and why primitives have fixed sizes on all platforms.

The 8 Primitive Data Types

Java defines 8 primitive types. Because Java is platform-independent, these primitives have fixed memory sizes and layouts across all CPUs and operating systems.

Type Size Default Value Value Range / Behavior
boolean JVM dependent false Representing true or false. Cannot be converted to numbers (unlike C++).
byte 1 byte (8-bit) 0 -128 to 127 (signed two's complement)
short 2 bytes (16-bit) 0 -32,768 to 32,767
char 2 bytes (16-bit) \u0000 0 to 65,535 (unsigned Unicode characters)
int 4 bytes (32-bit) 0 -2^31 to 2^31 - 1
long 8 bytes (64-bit) 0L -2^63 to 2^63 - 1
float 4 bytes (32-bit) 0.0f IEEE 754 single-precision floating-point
double 8 bytes (64-bit) 0.0d IEEE 754 double-precision floating-point

Memory Layout: Primitives vs References

Where variables are stored depends on their scope:

  • Local Variables (Stack): Local primitives store their numerical values directly inside stack frames. Local reference variables store the 4-byte or 8-byte reference address (pointer) pointing to the actual object instance on the heap.
  • Object Fields (Heap): Primitives and reference fields declared as object members live inside the object block on the heap.

Why Java Lacks Unsigned Primitives

To simplify programming and avoid type errors found in C++, Java's creators excluded unsigned integer types (except char). In C++, mixing signed and unsigned values in comparison operations is a frequent source of bugs. Java 8 introduced static methods in the Integer and Long wrapper classes (like Integer.compareUnsigned and Integer.divideUnsigned) to allow developers to perform unsigned arithmetic when needed.

Common Pitfalls

  • Floating Point Inaccuracy: Writing calculations like double val = 0.1 + 0.2; will result in a value like 0.30000000000000004 due to binary representation limits. Never use float or double for currency calculations. Use BigDecimal instead.
  • Primitive Overflows: Exceeding a primitive range will cause wrapping without throwing exceptions. For example, byte b = 127; b++; results in -128 due to signed two's complement math.

Best Practices

  • Use double for General Decimals: Prefer double over float for general decimal values to get higher precision and avoid constant float casting syntax (e.g. 3.14f).
  • Leverage BigDecimal for Financial Data: Always use java.math.BigDecimal for commercial calculations where exact rounding is required.

Interview-Relevant Information

Q1: What is the size of a boolean variable?
Answer: The size is not explicitly defined in the Java Virtual Machine Specification. In compiled code, a single boolean is usually represented by the JVM as a 32-bit int, while arrays of booleans are packed as 8-bit bytes (1 byte per element) to conserve memory.

Q2: Why does double conversion yield round-off errors?
Answer: Computers represent numbers in binary format. Fractions like 0.1 or 0.2 have infinite repeating representations in binary (similar to 1/3 in decimal), resulting in minor rounding errors.

Quick Checklist

Can you state the size of all 8 primitives, explain where local variables are stored, and name the alternative to float for monetary calculations? If yes, you are prepared for data type questions.

Use Cases

Using short/byte arrays to optimize memory usage in large-scale caching servers.

Choosing BigDecimal structures to prevent rounding errors in shopping cart checkouts.

Common Mistakes

Using floating point numbers (float/double) to store monetary or transaction currency values.

Failing to suffix large integer literals with an 'L' (e.g., long val = 3000000000; fails to compile because it exceeds int limits before casting).