Variable Types
Java is a statically typed language — the data type of each variable must be explicitly declared when the variable is declared, and it cannot be changed later. Unlike Python, which determines types automatically, Java forces the programmer to state the data type upfront, so type-related bugs are detected earlier, at compile time.
Java has two main categories of data types: primitive and reference (object). Primitive types store the value directly in memory (efficient), while reference types store the object's address on the heap. Primitive types cannot call methods — for that, use Wrapper Classes such as Integer and Double, which are converted automatically through the autoboxing feature.
Java also provides collection data types through the Collections Framework: ArrayList for dynamic lists, HashMap for key-value pairs, HashSet for unique values, and LinkedList for node-based data structures.
Primitive Data Types:
| Type | Size | Range | Example |
|---|---|---|---|
byte |
1 byte | -128 to 127 | byte x = 100; |
short |
2 bytes | -32,768 to 32,767 | short y = 30000; |
int |
4 bytes | -2 billion to 2 billion | int z = 100000; |
long |
8 bytes | Very large | long l = 9999999999L; |
float |
4 bytes | Low-precision decimal | float f = 3.14f; |
double |
8 bytes | High-precision decimal | double d = 3.14159; |
boolean |
1 bit | true/false | boolean active = true; |
char |
2 bytes | Unicode (0–65535) | char c = 'A'; |
Code Example
public class VariableTypes {
public static void main(String[] args) {
// Primitive types
byte age = 25;
short population = 50000;
int balance = 1500000;
long worldPopulation = 8_000_000_000L;
float pi = 3.14f;
double e = 2.71828;
boolean active = true;
char grade = 'A';
// Reference type
String name = "Java";
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Grade: " + grade);
// Check the type with instanceof
System.out.println(name + " is a String? " + (name instanceof String));
}
}
Code Explanation
The first few lines show variable declarations with various primitive types. Note the suffix L for long and f for float — this is required because by default Java treats decimal numbers as double. The name variable is of type String (a reference type). The + operator on Strings automatically performs concatenation. instanceof is the operator for checking an object's type at runtime.
Everyday Example
You build a simple shopping notes app, using various Java data types:
public class ShoppingNotes {
public static void main(String[] args) {
// Customer data
String customerName = "Alice";
double balance = 50000.0;
boolean member = true;
// Item prices (using arrays)
String[] shopping = {"apple", "bread", "milk", "eggs"};
int[] prices = {5000, 15000, 12000, 8000};
// Calculate the total
int total = 0;
for (int i = 0; i < shopping.length; i++) {
total += prices[i];
}
System.out.println("Customer: " + customerName);
System.out.println("Total spent: $" + total);
System.out.println("Remaining balance: $" + (balance - total));
}
}
Summary
Understanding data types is a crucial foundation in Java. Static typing gives your code safety and clarity, while Wrapper Classes and the Collections Framework provide the flexibility you need. Choose the right data type for each requirement for optimal performance and code readability.