In this Java tutorial, you will learn about primitive data types—including their default values, and memory sizes—with practical examples to illustrate each concept.
Data Types
In Java, data types are divided into two main categories: primitive data types and user-defined data types.
User-defined data types are typically implemented as classes, which we will explore in detail in our Java Object Oriented Programming tutorials.
1 What is a Data Type?
Data type specifies the kind of value a variable can hold, the amount of memory allocated for it, and the range of values it can represent. This helps ensure that data is used consistently and efficiently in your programs.
2 List of Primitive Data Types in Java
Java provides eight primitive data types:
Although a sequence of characters (a string) is not a primitive data type, Java offers extensive support for strings through the java.lang.String class.
3 Primitive Data Types – Size & Default Values
The table below summarizes each primitive data type’s memory size, default value, and whether it uses 2’s complement representation (for integral types):
Data Type | Size | Default Value | 2’s Complement (Signed) |
---|---|---|---|
byte | 8 bits | 0 | Yes |
short | 16 bits | 0 | Yes |
int | 32 bits | 0 | Yes |
long | 64 bits | 0L | Yes |
float | 32-bit IEEE 754 | 0.0f | N/A |
double | 64-bit IEEE 754 | 0.0d | N/A |
boolean | 1 bit | false | N/A |
char | 16-bit Unicode | ‘\u0000’ | N/A |
Note #1: In Java SE8 and later, the Integer
class provides methods that allow you to treat an int
as an unsigned integer.
Note #2: Instance and class variables receive default values if not explicitly initialized. However, local variables are not automatically initialized, so you must assign a value before using them to avoid compile-time errors.
Note #3: The term signed indicates that the data type can represent both negative and positive values, whereas unsigned means it can only represent positive values.
4 Data Type of a Variable
Java is a statically-typed language, which means every variable must be declared with a specific data type before it is used. This ensures that the variable stores and operates on values consistent with its declared type. Attempting to redeclare a variable with a different data type will result in a compile-time error.
For example, to declare a variable n
as an int
, you would write:
int n;
Once declared, the variable n
will always be treated as an integer throughout its lifetime.
5 Initializing Variables
It is a good practice to initialize variables so that they have a well-defined starting state, which helps prevent errors. The table below provides examples of initializing variables for different data types:
Data Type | Example Initialization |
---|---|
byte | byte b = 65; |
short | short si = 456; |
int | int i = 10; |
long | long li = 25589L; |
float | float f = 5.6f; |
double | double d = 5.63128e3; |
boolean | boolean isTodayHoliday = false; |
char | char ch = 'A'; |
String | String str = "Good day!"; |
The following program demonstrates how to initialize variables of various data types:
Example.java
public class Example {
public static void main(String[] args) {
byte b = 65;
short s = 456;
int i = 10;
long l = 25589L;
float f = 5.6f;
double d = 5.63128e3;
boolean bool = false;
char ch = 'A';
String str = "Good day!";
}
}
Conclusion
In this Java Tutorial, we explored Java’s data types, including the eight primitive types, their memory sizes, default values, and how to declare and initialize them. Mastery of these fundamentals will help you write robust and error-free Java programs.