Java provides several primitive data types for storing numeric values. The byte, short, int, and long data types are used for whole numbers, while float is used for decimal numbers.
Understanding these data types helps you choose the correct type when creating variables and performing calculations in Java.
Java byte
The byte data type is an 8-bit signed integer. It can store whole numbers from -128 to 127.
For example, byte age = 25;
The byte type is useful when you specifically need a small range of integer values or when working with binary data.
public class Main {
public static void main(String[] args) {
byte age = 25;
System.out.println(age);
}
}
Java short
The short data type is a 16-bit signed integer. Its range is -32,768 to 32,767.
For example, short marks = 25000;
It can store larger values than byte, but it is smaller than int.
public class Main {
public static void main(String[] args) {
short marks = 25000;
System.out.println(marks);
}
}
In normal Java applications, short is less commonly used because int is generally preferred for ordinary integer calculations.
Java int
The int data type is a 32-bit signed integer. Its range is -2,147,483,648 to 2,147,483,647.
For example, int age = 25;
int is the most commonly used type for whole numbers in Java.
public class Main {
public static void main(String[] args) {
int age = 25;
int marks = 85;
int salary = 50000;
System.out.println(age);
System.out.println(marks);
System.out.println(salary);
}
}
For most ordinary integer values, int is the recommended choice unless there is a specific reason to use another type.
Java long
The long data type is a 64-bit signed integer. It is used when a number may exceed the range of int.
Its range is approximately -9.22 quintillion to 9.22 quintillion.
For example, long population = 5000000000L;
The L suffix indicates that the number is a long literal.
public class Main {
public static void main(String[] args) {
long population = 5000000000L;
System.out.println(population);
}
}
The long type is useful for large numbers such as large counts, timestamps, distances, and other values that may exceed the int range.
Java float
The float data type is a 32-bit single-precision floating-point type. It is used to store decimal values.
For example, float price = 99.5f;
The f suffix is required when assigning a decimal literal directly to a float.
public class Main {
public static void main(String[] args) {
float price = 99.5f;
System.out.println(price);
}
}
float uses less memory than double, but it provides less precision. For most general-purpose decimal calculations, double is usually preferred.
Java double Data Type
The double data type in Java is used to store decimal or floating-point numbers. It is a 64-bit (8-byte) double-precision data type and provides approximately 15–17 significant digits of precision.
For example:
double price = 99.99;
The double type is the default data type for decimal values in Java, so you do not need to add a suffix.
double temperature = 36.5;
System.out.println(temperature);
The double type is commonly used for measurements, percentages, scientific calculations, and mathematical operations. However, it may produce small rounding differences because decimal values are represented in binary. For exact decimal calculations, BigDecimal is more suitable.
Comparison of Java Data Types
| Data Type | Size | Type | Range / Precision | Example |
|---|---|---|---|---|
byte | 8 bits | Integer | -128 to 127 | byte n = 10; |
short | 16 bits | Integer | -32,768 to 32,767 | short n = 1000; |
int | 32 bits | Integer | -2.14 billion to 2.14 billion | int n = 50000; |
long | 64 bits | Integer | About ±9.22 quintillion | long n = 5000000000L; |
float | 32 bits | Decimal | About 6-7 decimal digits of precision | float n = 10.5f; |
Which Data Type Should You Use?
For most whole-number calculations, int is the best general-purpose choice. Use long when the value can exceed the int range.
Use byte or short when their specific ranges are appropriate, especially in situations where memory representation matters.
For decimal values, float can be used when single precision is sufficient. However, double is generally preferred for most everyday decimal calculations.