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:

  1. byte
  2. short
  3. int
  4. long
  5. float
  6. double
  7. boolean
  8. char

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 TypeSizeDefault Value2’s Complement (Signed)
byte8 bits0Yes
short16 bits0Yes
int32 bits0Yes
long64 bits0LYes
float32-bit IEEE 7540.0fN/A
double64-bit IEEE 7540.0dN/A
boolean1 bitfalseN/A
char16-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:

</>
Copy
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 TypeExample Initialization
bytebyte b = 65;
shortshort si = 456;
intint i = 10;
longlong li = 25589L;
floatfloat f = 5.6f;
doubledouble d = 5.63128e3;
booleanboolean isTodayHoliday = false;
charchar ch = 'A';
StringString str = "Good day!";

The following program demonstrates how to initialize variables of various data types:

Example.java

</>
Copy
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.