Summary: in this tutorial, you’ll learn about Java primitive data types including integers, floats, characters, and Boolean.
Introduction to the Java primitive data types
Primitive data types are built into the Java language. They’re the foundation of all other types.
Java divides the primitive types into four categories:
- Integer types
- Floating-point types
- Character type
- Boolean type
If you define a variable with these primitive types, the variable can hold a single value.
Integer types
There are four integer types in Java. The differences between them are the range of values they can store and the storage they can take up.
The following table shows four integer types including byte, short, int, and long:
Type | Size (bits) | Minimum | Maximum | Default | Example |
---|---|---|---|---|---|
byte | 8 | -27 | 27– 1 | 0 | byte age = 100; |
short | 16 | -215 | 215– 1 | 0 | short pages = 2_000; |
int | 32 | -231 | 231– 1 | 0 | int |
long | 64 | -263 | 263– 1 | 0 | long |
Notice that the long
type has the literal form ending with the letter L
. All integer (and float) literals can use the underscore (_
) as the number separator to make the code more readable.
Floating-point types
Floating-point types store values containing a fractional portion. There are two floating-point types: float
and double
.
Type | Size (bits) | Minimum | Maximum | Default | Example |
---|---|---|---|---|---|
float | 32 | -2-149 | (2-2-23)·2127 | 0.0f | float f = |
double | 64 | -2-1074 | (2-2-52)·21023 | 0.0 | double f = |
float
The float
data type represents single-precision floating-point numbers. It is a 32-bit data type that can store numbers with a fractional part.
In practice, you use the float
type to represent numbers with a greater range and precision than integer types.
The following shows how to declare and initialize a float
variable in Java:
float pi = 3.14f;
Code language: Java (java)
Note that the literal form of a float has the f
suffix. If you don’t use the 'f'
suffix, Java assumes that the type of the value is double. So adding 'f'
ensures that it’s treated as a float.
If you declare a float variable but don’t initialize its value, the default of the float variable is 0.0
.
double
The double
data type represents double-precision floating-point numbers. It’s a 64-bit data type and provides higher precision than the float data type, which is single-precision floating-point numbers.
The following shows how to declare and initialize a double
variable:
double amount = 12345.67;
Code language: Java (java)
Unlike float, you don’t need to suffix when initializing a double
variable because Java assumes that numeric literals with decimal points are of type double
by default.
If you want to be explicit, you can use the D
suffix to decimal literal like this:
double amount = 12345.67D;
Code language: Java (java)
Like the float, the default value of a double variable is 0.0
In practice, you use double
data type when it requires a high level of precision in calculations involving decimal numbers. It is suitable for most scientific, engineering, and financial applications where accuracy is critical.
The downside is that the double
type consumes more memory compared to float
due to its larger size. Therefore, you should consider memory constraints when deciding between double
and float
for your variables.
Character type
The char
type represents a single character. It’s a 16-bit unsigned data type that can store any character from a Unicode character set, including letters, digits, symbols, and special characters.
Type | Size (bits) | Minimum | Maximum | Default | Example |
---|---|---|---|---|---|
char | 16 | 0 | 216– 1 | '/u0000' | char ch = ‘A'; |
The following shows how to declare a variable with type char
:
char ch = 'A';
System.out.println(ch); // A
// Represents the Greek letter Omega (Ω)
ch = '\u03A9';
System.out.println(ch); // Ω
Code language: Java (java)
In this example, we define the variable ch
as the char
variable and initialize its value to the character 'A'
. Then we assign a Unicode character with the code point '039A'
, which corresponds to the Greek letter Omega (Ω). For a complete list of Unicode characters, please visit this link.
Boolean type
The Java boolean
type has two possible values: true or false. Java uses one bit to store the boolean
values.
Type | Size (bits) | Minimum | Maximum | Default | Example |
---|---|---|---|---|---|
boolean | 1 | – | – | false | boolean pending = true; |
The following shows how to define two boolean
variables and initialize their values to false
and true
:
boolean isCompleted = false;
boolean pending = true;
Code language: Java (java)
In practice, you use the boolean
type to express logical states or the conditions in programs. And you use them to control the flow of code execution through conditional statements like if
, while
, and for
.
Primitive types are stored by value
Let’s take a simple example to understand how Java stores primitive values:
public class App {
public static void main(String[] args) {
int x = 10;
int y = x;
System.out.println(x);
System.out.println(y);
y = 20;
System.out.println(x);
System.out.println(y);
}
}
Code language: Java (java)
Output:
10
10
10
20
Code language: plaintext (plaintext)
How it works.
First, declare a variable x
and initialize its values to 10
:
int x = 10;
Code language: Java (java)
Second, declare another variable y
and assign the value of x
to y
:
int y = x;
Behind the scenes, Java copies the primitive value stored in the x
variable to the y
variable. Both variables store the value 10
.
Third, display values of both x
and y
variables. It shows that both variables are 10
:
System.out.println(x);
System.out.println(y);
Code language: Java (java)
Also, there is no link between the two variables. In other words, if you change the value of the variable y
, it doesn’t affect the original value stored in the variable x
:
Fourth, change the value of y to 20:
y = 20;
Code language: Java (java)
Finally, display the values of the variables x
and y
, the value of the variable x
doesn’t change while the value of y
changes:
System.out.println(x);
System.out.println(y);
Code language: Java (java)
It shows the value of x is 10 and the value of y is 20.
In conclusion, Java stores the primitive types by value, which means when you assign one primitive variable to another, you’re making a copy of the actual value.
Summary
- Java primitive types include integer types, float types, character type, and boolean type.
- Variables of primitive types can hold a single value.
- Java stores primitive types by value.