Java Data Types
In Java, every value has a type. A data type tells the program what kind of data a variable can hold — such as a number, a character, or a true/false value. Java is a strongly typed language, meaning the data type of every variable must be declared before it is used.
Data types in Java are divided into two main categories: Primitive Data Types and Non-Primitive (Reference) Data Types.
1. Primitive Data Types
Primitive data types are the most basic building blocks of data in Java. There are 8 primitive data types in Java. Each one stores a specific kind of value and occupies a fixed amount of memory.
Overview Table
| Data Type | Size | Default Value | Example Value | Description |
|---|---|---|---|---|
| byte | 1 byte | 0 | 100 | Very small whole numbers |
| short | 2 bytes | 0 | 30000 | Small whole numbers |
| int | 4 bytes | 0 | 1500000 | Standard whole numbers |
| long | 8 bytes | 0L | 9876543210L | Large whole numbers |
| float | 4 bytes | 0.0f | 3.14f | Decimal numbers (less precise) |
| double | 8 bytes | 0.0d | 3.14159265 | Decimal numbers (more precise) |
| char | 2 bytes | '\u0000' | 'A' | A single character |
| boolean | 1 bit | false | true / false | True or false values only |
byte
The byte data type stores very small whole numbers. Its range is from -128 to 127. It is useful when saving memory is important, such as in large arrays.
public class DataTypeDemo {
public static void main(String[] args) {
byte age = 25;
System.out.println("Age: " + age);
}
}short
The short data type stores small whole numbers. Its range is from -32,768 to 32,767. It is rarely used in modern Java.
short temperature = -200;
System.out.println("Temperature: " + temperature);int
The int data type is the most commonly used type for whole numbers. Its range is from -2,147,483,648 to 2,147,483,647. Unless there is a specific reason to use another integer type, int is the default choice.
int salary = 50000;
System.out.println("Salary: " + salary);long
The long data type is used when numbers exceed the range of int. Long values must be followed by the letter L to distinguish them from int.
long worldPopulation = 8000000000L;
System.out.println("World Population: " + worldPopulation);float
The float data type stores decimal numbers with up to 7 significant digits. Float values must be followed by the letter f.
float price = 19.99f;
System.out.println("Price: " + price);double
The double data type is the default for decimal numbers and is more precise than float — it supports up to 15 significant digits. It does not need a suffix.
double piValue = 3.14159265358979;
System.out.println("Pi: " + piValue);char
The char data type stores a single character. The value must be enclosed in single quotes.
char grade = 'A';
System.out.println("Grade: " + grade);Java stores characters as Unicode values. So 'A' is actually stored as the number 65 internally.
boolean
The boolean data type holds only two possible values: true or false. It is commonly used in conditions and decision-making.
boolean isLoggedIn = true;
boolean hasPaid = false;
System.out.println("Logged in: " + isLoggedIn);
System.out.println("Has paid: " + hasPaid);2. Non-Primitive (Reference) Data Types
Non-primitive data types are not predefined by Java — they are created by the programmer. They refer to objects stored in memory, which is why they are also called reference types.
Common non-primitive data types include:
- String: A sequence of characters. Example:
"Hello, Java!" - Arrays: A collection of values of the same type.
- Classes: User-defined blueprints for creating objects.
- Interfaces: Abstract contracts for classes to implement.
String name = "Alice";
System.out.println("Name: " + name);Unlike primitive types, non-primitive types can be null — meaning they point to no object at all.
Difference Between Primitive and Non-Primitive
| Feature | Primitive | Non-Primitive |
|---|---|---|
| Defined by | Java language | Programmer or Java library |
| Stores | Actual value | Reference (address) to an object |
| Default value | Has a default (e.g., 0, false) | null |
| Examples | int, char, boolean | String, Array, Class |
Complete Example
public class AllDataTypes {
public static void main(String[] args) {
byte b = 10;
short s = 500;
int i = 75000;
long l = 9000000000L;
float f = 5.75f;
double d = 19.99999;
char c = 'J';
boolean flag = true;
String text = "Java is fun";
System.out.println("byte: " + b);
System.out.println("short: " + s);
System.out.println("int: " + i);
System.out.println("long: " + l);
System.out.println("float: " + f);
System.out.println("double: " + d);
System.out.println("char: " + c);
System.out.println("boolean: " + flag);
System.out.println("String: " + text);
}
}Output:
byte: 10
short: 500
int: 75000
long: 9000000000
float: 5.75
double: 19.99999
char: J
boolean: true
String: Java is funSummary
- Java has 8 primitive data types:
byte,short,int,long,float,double,char, andboolean. - Use
intfor whole numbers anddoublefor decimal numbers in most cases. - Use
charfor a single character andbooleanfor true/false values. - Non-primitive types include String, arrays, and classes — they store references to objects.
- Java is strongly typed: every variable must have a declared type before use.
