Web Analytics

Java Primitive Data Types

Beginner ~25 min read

Data types are the foundation of any programming language. They tell the compiler what kind of data a variable holdsโ€”whether it's a number, a character, or a true/false value. Java has 8 primitive data types that act as the building blocks for data manipulation.

The 8 Primitive Data Types

Java is a strongly-typed language, meaning every variable must have a declared type. The 8 primitive types are categorized into four groups:

Java Primitive Data Types Comparison
Type Group Size Description
byte Integer 1 byte Very small logical numbers (-128 to 127)
short Integer 2 bytes Small numbers (-32,768 to 32,767)
int Integer 4 bytes Standard integer (default for whole numbers)
long Integer 8 bytes Large integers (requires 'L' suffix)
float Floating Point 4 bytes Decimal numbers (requires 'f' suffix)
double Floating Point 8 bytes Precise decimals (default for decimals)
boolean Other 1 bit* True or false values
char Other 2 bytes Single character (Unicode)

* The size of boolean is not precisely defined by the JVM specification, but conceptually represents 1 bit of information.

Integer Types (Whole Numbers)

Integer types store whole numbers without decimals. Use int for most counting needs. Use long when int isn't big enough.

Output
Click Run to execute your code
The 'L' Suffix: When declaring a long variable, you MUST append an 'L' or 'l' to the number if it exceeds the range of int. Otherwise, the compiler treats it as an integer and throws an error if it's too large.
long bigNum = 10000000000L; // Correct

Floating Point Types (Decimals)

Floating point types store numbers with fractional parts. double is the default choice for modern applications as it's more precise than float.

  • float: Single precision (6-7 significant decimal digits). Suffix 'f' required (e.g., 3.14f).
  • double: Double precision (15-16 significant decimal digits). No suffix needed.

Financial Calculations

Never use float or double for currency! Floating-point arithmetic can have rounding errors (e.g., 0.1 + 0.2 might equal 0.30000000000000004). For precise calculations like money, use the BigDecimal class.

Boolean & Character Types

Boolean

The boolean data type has only two possible values: true or false. It's essential for logic and decision making.

boolean isActive = true;
boolean isGameOver = false;

Char

The char data type stores a single 16-bit Unicode character. It must be enclosed in single quotes ' '.

char grade = 'A';
char symbol = '$';

Size & Range Demonstration

Let's use Java's wrapper classes to inspect the minimum and maximum values for each numeric type.

Output
Click Run to execute your code

Summary

  • int is the go-to type for whole numbers.
  • double is the default for decimal numbers.
  • boolean stores simply true or false.
  • char stores a single character using single quotes.
  • Remember suffixes: L for long literals, f for float literals.

What's Next?

Now that you know the available types, you need to learn how to store them! In the next lesson, we'll dive deep into Variablesโ€”how to declare, initialize, and name them correctly.