Java Data Types

Java data types specify the type of value that a variable can store. When you create a variable in Java, you need to tell Java what kind of data the variable will contain.

For example, int age = 25; declares an integer variable named age.

Java data types are mainly divided into two categories:

  1. Primitive Data Types
  2. Non-Primitive Data Types

Primitive Data Types

Java has 8 primitive data types. They are used to store simple values directly.

Data TypeSizeExampleUsed For
byte8 bits100Small whole numbers
short16 bits10000Whole numbers
int32 bits50000Common whole numbers
long64 bits5000000000LLarge whole numbers
float32 bits10.5fDecimal values
double64 bits10.5Precise decimal values
char16 bits'A'Single characters
boolean—trueTrue or false values

Integer Data Types

Java provides four types for storing whole numbers: byte, short, int, and long.

The byte type stores small integer values. Its range is -128 to 127.

The short type stores values from -32,768 to 32,767.

The int type is the most commonly used integer type. It can store values from -2,147,483,648 to 2,147,483,647.

The long type is used for very large integer values.

For example:

public class Main {
    public static void main(String[] args) {
        int age = 25;
        long population = 5000000000L;

        System.out.println(age);
        System.out.println(population);
    }
}

Notice the L after the large number. It indicates a long literal.

Floating-Point Data Types

Java provides float and double for storing decimal numbers.

A float uses 32 bits, while a double uses 64 bits and generally provides greater precision.

For example, float price = 99.5f; and double salary = 45000.75;.

double is generally preferred for ordinary decimal calculations.

Character Data Type

The char type is used to store a single UTF-16 code unit.

For example, char grade = 'A';.

Characters are written inside single quotation marks.

public class Main {
    public static void main(String[] args) {
        char grade = 'A';

        System.out.println(grade);
    }
}

Boolean Data Type

The boolean type stores one of two values: true or false.

For example, boolean isPassed = true;.

It is commonly used in conditions and decision-making statements.

Non-Primitive Data Types

Non-primitive data types are also called reference types. Unlike primitive types, they refer to objects or other reference values.

Common examples include:

TypeExample
StringString name = "Karim";
Arrayint[] numbers = {1, 2, 3};
ClassStudent student = new Student();
InterfaceReference to an implementing object

String is one of the most commonly used reference types in Java.

For example:

public class Main {
    public static void main(String[] args) {
        String name = "Karim";

        System.out.println(name);
    }
}

Primitive vs Non-Primitive Data Types

PrimitiveNon-Primitive
Built into JavaCreated from classes, interfaces, arrays, etc.
Stores basic valuesHolds references to objects or other reference values
8 typesMany types
Examples: int, double, charExamples: String, arrays, classes