Java Data Types — Primitives, Strings & Type Sizes
In the previous chapter, you learned that every variable has a type. Now let’s explore all the types Java offers and when to use each one.
Two Categories of Data Types
Java data types fall into two categories:
Java Data Types
├── Primitive Types (8 types — stored directly in memory)
│ ├── Integer: byte, short, int, long
│ ├── Floating-point: float, double
│ ├── Character: char
│ └── Boolean: boolean
│
└── Reference Types (point to objects in memory)
├── String
├── Arrays
├── Classes
└── Interfaces
Primitive Types — The Complete List
| Type | Size | Range | Default | Example |
|---|---|---|---|---|
byte |
1 byte | -128 to 127 | 0 | byte b = 100; |
short |
2 bytes | -32,768 to 32,767 | 0 | short s = 30000; |
int |
4 bytes | -2.1 billion to 2.1 billion | 0 | int age = 25; |
long |
8 bytes | ±9.2 quintillion | 0L | long pop = 8000000000L; |
float |
4 bytes | ~7 decimal digits | 0.0f | float pi = 3.14f; |
double |
8 bytes | ~15 decimal digits | 0.0 | double price = 29.99; |
char |
2 bytes | Single Unicode character | ‘\u0000’ | char grade = 'A'; |
boolean |
1 bit* | true or false |
false | boolean done = true; |
*The actual size of
booleanin memory depends on the JVM implementation.
Integer Types — byte, short, int, long
When to Use What
| Type | Use When |
|---|---|
int |
Default choice for whole numbers — 99% of the time |
long |
Numbers bigger than 2.1 billion (populations, timestamps) |
byte |
Memory-sensitive work (large arrays, file I/O) |
short |
Rarely used — int is fine |
Examples
public class IntegerTypes {
public static void main(String[] args) {
byte smallNumber = 100;
short mediumNumber = 30000;
int regularNumber = 2000000000;
long bigNumber = 9000000000L; // Note the 'L' suffix
System.out.println("byte: " + smallNumber);
System.out.println("short: " + mediumNumber);
System.out.println("int: " + regularNumber);
System.out.println("long: " + bigNumber);
}
}
Output:
byte: 100
short: 30000
int: 2000000000
long: 9000000000
Important:
longvalues need anLsuffix. Without it, Java treats the number asintand you’ll get a compile error for values > 2.1 billion.
Integer Overflow — What Happens When You Go Past the Limit
int maxInt = 2147483647; // Maximum int value
System.out.println(maxInt); // 2147483647
System.out.println(maxInt + 1); // -2147483648 ← Overflow! Wraps around
This is called integer overflow. The number wraps around to the minimum value. Java does NOT warn you about this.
Floating-Point Types — float, double
When to Use What
| Type | Use When |
|---|---|
double |
Default choice for decimal numbers — 99% of the time |
float |
Memory-sensitive work (graphics, large arrays) |
Examples
public class FloatingPoint {
public static void main(String[] args) {
float temperature = 98.6f; // 'f' suffix required
double price = 19.99; // No suffix needed (default is double)
double pi = 3.141592653589793;
System.out.println("Temperature: " + temperature);
System.out.println("Price: $" + price);
System.out.println("Pi: " + pi);
}
}
Output:
Temperature: 98.6
Price: $19.99
Pi: 3.141592653589793
The Floating-Point Precision Problem
System.out.println(0.1 + 0.2); // 0.30000000000000004 ← Not 0.3!
This isn’t a Java bug — it’s how floating-point numbers work in all programming languages. Binary can’t represent some decimal fractions exactly.
For money, use BigDecimal:
import java.math.BigDecimal;
BigDecimal price1 = new BigDecimal("0.1");
BigDecimal price2 = new BigDecimal("0.2");
System.out.println(price1.add(price2)); // 0.3 ← Exact!
Character Type — char
A char holds a single character using single quotes:
char letter = 'A';
char digit = '7';
char symbol = '@';
char newline = '\n'; // Escape sequence
char unicode = '\u0041'; // Unicode for 'A'
System.out.println(letter); // A
System.out.println(digit); // 7
Common escape sequences:
| Escape | Meaning |
|---|---|
\n |
Newline |
\t |
Tab |
\\ |
Backslash |
\" |
Double quote |
\' |
Single quote |
Remember: single quotes
'A'forchar, double quotes"Alice"forString.
Boolean Type — boolean
boolean isJavaFun = true;
boolean isRaining = false;
System.out.println("Java is fun: " + isJavaFun); // Java is fun: true
System.out.println("Is raining: " + isRaining); // Is raining: false
Booleans are used extensively in conditions (if/else, loops). We’ll cover this in the Boolean Expressions chapter.
String — A Reference Type
String is NOT a primitive — it’s a class (reference type). But it’s so commonly used that Java gives it special treatment:
String greeting = "Hello, World!";
String name = "Alice";
String empty = "";
System.out.println(greeting); // Hello, World!
System.out.println(name.length()); // 5
System.out.println(name.toUpperCase()); // ALICE
Strings are immutable — once created, they can’t be changed. Methods like toUpperCase() return a new String.
We cover strings in depth in Module 3: Arrays, Strings & Utilities.
Default Values
When you declare instance variables (class fields) without initializing them, Java assigns default values:
| Type | Default Value |
|---|---|
byte, short, int, long |
0 |
float, double |
0.0 |
char |
'\u0000' (null character) |
boolean |
false |
| Reference types (String, arrays, objects) | null |
Important: Local variables (inside methods) do NOT get default values. You must initialize them before use.
Numeric Literals — Readability Tricks
Java allows underscores in numeric literals (since Java 7) for readability:
int million = 1_000_000; // Same as 1000000
long creditCard = 1234_5678_9012_3456L;
double pi = 3.14_15_92;
int binary = 0b1010_1010; // Binary literal
int hex = 0xFF_EC_DE; // Hexadecimal literal
Common Mistakes
Mistake: Using float Without the f Suffix
float price = 9.99; // Error! 9.99 is a double by default
float price = 9.99f; // Correct!
Mistake: Putting a String in an int
int age = "25"; // Error! "25" is a String, not an int
int age = 25; // Correct!
Mistake: Using == to Compare Strings
String a = "hello";
String b = "hello";
System.out.println(a == b); // true (but only by luck — string pool)
System.out.println(a.equals(b)); // true (always correct)
Always use .equals() for String comparison. We’ll explain why in the Boolean Expressions chapter.
Try It Yourself
Create a program that stores and prints information about a product:
- Name (String)
- Price (double)
- Quantity in stock (int)
- Is available (boolean)
- Category code (char)
Summary
| Type | Category | Size | Use Case |
|---|---|---|---|
int |
Primitive | 4 bytes | Most whole numbers |
long |
Primitive | 8 bytes | Very large numbers |
double |
Primitive | 8 bytes | Most decimal numbers |
float |
Primitive | 4 bytes | Memory-sensitive decimals |
char |
Primitive | 2 bytes | Single characters |
boolean |
Primitive | 1 bit | true/false |
String |
Reference | Varies | Text |
Rule of thumb: Use int for whole numbers, double for decimals, boolean for true/false, and String for text.
What’s Next?
What if you need to convert between types — like turn an int into a double? That’s called type casting: