Understanding Data Types

Understanding Data Types

OCP Exam Topic — Know all eight primitive types, their sizes and ranges, wrapper classes, String, and text blocks. Covered in Chapter 1 of OCP Java SE 21 Developer Study Guide (Selikoff & Boyarsky).


Primitives vs Reference Types

Java has two categories of data type:

Category Stored Examples
Primitive Value directly in memory int, boolean, double
Reference Address (pointer) to an object String, Animal, int[]

Primitives are lowercase; classes start with an uppercase letter.


The Eight Primitive Types

Type Size Range / Notes Default
byte 8 bits −128 to 127 0
short 16 bits −32,768 to 32,767 0
int 32 bits −2,147,483,648 to 2,147,483,647 0
long 64 bits −9.2 × 10¹⁸ to 9.2 × 10¹⁸ 0L
float 32 bits ~7 decimal digits of precision 0.0f
double 64 bits ~15 decimal digits of precision 0.0
boolean not specified true or false false
char 16 bits '' to '￿' (0–65,535) ''

The JVM specification does not define the physical size of boolean; the table reflects typical implementations. What matters for the exam is the value set: true or false only.


Numeric Literals

Java allows underscores in numeric literals to improve readability (Java 7+):

java
int million = 1_000_000; long big = 9_223_372_036_854_775_807L; // L suffix for long double price = 1_999.99;

Literal bases:

java
int decimal = 58; // base 10 (default) int octal = 072; // base 8 — starts with 0 int hex = 0x3A; // base 16 — starts with 0x or 0X int binary = 0b111010; // base 2 — starts with 0b or 0B

Suffix rules:

  • L or l — long literal (100L). Uppercase L is preferred (lowercase looks like 1).
  • F or f — float literal (3.14f).
  • D or d — double literal (3.14d); optional since double is the default for decimal literals.

Floating-Point Precision

float and double use IEEE 754 binary representation. Exact decimal values are often not representable:

java
System.out.println(0.1 + 0.2); // prints 0.30000000000000004

For financial calculations requiring exact decimal arithmetic, use java.math.BigDecimal, not double.


char

char stores a single Unicode code unit (UTF-16). It can be assigned as a character literal, a Unicode escape, or an integer:

java
char letter = 'A'; char unicode = 'A'; // also 'A' char num = 65; // also 'A'

char is an unsigned 16-bit integer, so arithmetic is valid:

java
char c = 'A'; System.out.println(c + 1); // prints 66 (int arithmetic) System.out.println((char)(c + 1)); // prints 'B'

Wrapper Classes

Every primitive has a corresponding wrapper class in java.lang:

Primitive Wrapper
byte Byte
short Short
int Integer
long Long
float Float
double Double
boolean Boolean
char Character

Wrappers allow primitives to be used where objects are required (e.g., List<Integer>). Autoboxing and unboxing convert between primitives and wrappers automatically:

java
Integer boxed = 42; // autoboxing: int → Integer int unboxed = boxed; // unboxing: Integer → int List<Integer> numbers = new ArrayList<>(); numbers.add(10); // autoboxing happens here int first = numbers.get(0); // unboxing happens here

String

String is a reference type (an object), not a primitive. It is immutable — once created, its character sequence cannot change.

java
String greeting = "Hello"; greeting = greeting + " World"; // creates a NEW String; "Hello" is unchanged

String literals are stored in the string pool (a special area of heap memory). Two literals with the same content share the same object:

java
String a = "Hello"; String b = "Hello"; System.out.println(a == b); // true — same pool object System.out.println(a.equals(b)); // true — same content String c = new String("Hello"); System.out.println(a == c); // false — c is a new heap object outside the pool System.out.println(a.equals(c)); // true — same content

Text Blocks (Java 15+)

A text block is a multi-line string literal delimited by triple double-quotes """:

java
String json = """ { "name": "Alice", "age": 30 } """;

Key rules the exam tests:

  • The opening """ must be followed by a newline (no content on the same line).
  • Incidental leading whitespace is stripped based on the position of the closing """.
  • The closing """ on its own line adds a trailing newline; placing it at the end of the last content line removes it.
java
String noTrailingNewline = """ Hello"""; // "Hello" — no trailing newline String withTrailingNewline = """ Hello """; // "Hello\n"

Key Points to Remember

  • There are exactly eight primitive types; know their sizes and default values.
  • long literals need an L suffix; float literals need an F suffix.
  • Underscores can appear between digits in numeric literals but not at the start, end, or adjacent to a decimal point.
  • char is an unsigned 16-bit integer and can participate in arithmetic.
  • Each primitive has a wrapper class in java.lang; autoboxing/unboxing converts between them automatically.
  • String is immutable; concatenation creates a new object.
  • String literals with the same content share the same pool object; new String(...) bypasses the pool.
  • Text blocks strip incidental whitespace and are available since Java 15.