Java Data Types
A data type tells Java what kind of value a variable can store. Java uses types to understand how much memory a value needs and what operations are allowed.
For example, a whole number, a decimal number, one character, and a true-or-false value are different kinds of data. Choosing the right type makes your program clearer and helps Java catch mistakes early.
Common Java Data Types
Java has primitive data types for basic values. It also has reference types, such as String, for objects. As a beginner, you will use these types often:
| Type | Stores | Example value |
|---|---|---|
int |
Whole numbers | 42 |
long |
Large whole numbers | 9000000000L |
double |
Decimal numbers | 19.99 |
float |
Smaller decimal numbers | 0.5f |
char |
One character | 'A' |
boolean |
true or false |
true |
String |
Text | "Java" |
Example
The program below declares variables with several different data types, then prints their values.
public class Main {
public static void main(String[] args) {
int students = 28;
long cityPopulation = 8804190L;
double price = 19.99;
float progress = 0.75f;
char grade = 'A';
boolean passed = true;
String course = "Java";
System.out.println("Students: " + students);
System.out.println("Population: " + cityPopulation);
System.out.println("Price: " + price);
System.out.println("Progress: " + progress);
System.out.println("Grade: " + grade);
System.out.println("Passed: " + passed);
System.out.println("Course: " + course);
}
}
Output:
Students: 28
Population: 8804190
Price: 19.99
Progress: 0.75
Grade: A
Passed: true
Course: Java
How It Works
The variable students uses int because it stores a normal whole number. The variable cityPopulation uses long because population values can become too large for int. A long literal usually ends with L.
The variable price uses double, Java’s common type for decimal numbers. The variable progress uses float, which also stores decimals, but a float literal must end with f.
The variable grade uses char, which stores exactly one character in single quotes. The variable passed uses boolean, which can only be true or false. The variable course uses String for text in double quotes.
Primitive Types
Java has eight primitive data types: byte, short, int, long, float, double, char, and boolean. Primitive values store simple data directly.
byte smallNumber = 100;
short year = 2026;
int score = 95;
long distance = 384400000L;
float ratio = 0.5f;
double total = 123.45;
char initial = 'M';
boolean active = true;
For most whole numbers, use int. For most decimal numbers, use double. Use the smaller or more specific types when you have a clear reason.
String Is Not Primitive
String is not one of the eight primitive types. It is a class used to store text. That is why String starts with an uppercase S, unlike primitive type names such as int and boolean.
A String can contain zero, one, or many characters. A char can contain only one character.
Choosing A Type
- Use
intfor ordinary whole numbers. - Use
longfor very large whole numbers. - Use
doublefor ordinary decimal numbers. - Use
charfor one character. - Use
booleanfor yes-or-no conditions. - Use
Stringfor words, sentences, names, and other text.
Takeaway: every Java variable has a data type, and the type should match the kind of value you want the variable to store.
