Skip to content

Java Data Types

In Java, every variable has a type that determines what kind of data the variable can store and what operations can be performed on that data. Java's data types are divided into two main categories: Primitive Data Types and Reference Data Types.

1. Primitive Data Types

Primitive data types are the most basic data types built into the Java language. They are not objects and directly store data values. There are 8 primitive data types in Java.

Integer Types

Used to represent numbers without a decimal part.

  • byte:

    • Size: 1 byte (8 bits)
    • Range: -128 to 127
    • Usage: Used when memory needs to be saved and the value range is small, such as when handling file streams or binary data.
  • short:

    • Size: 2 bytes (16 bits)
    • Range: -32,768 to 32,767
    • Usage: Also used to save memory, with a larger range than byte.
  • int:

    • Size: 4 bytes (32 bits)
    • Range: -2,147,483,648 to 2,147,483,647
    • Usage: The most commonly used integer type. By default, any integer you write (like 100) is treated as an int type.
  • long:

    • Size: 8 bytes (64 bits)
    • Range: Very large
    • Usage: Used when int is not enough to store a very large integer. Note: long values must have L or l at the end. For example: long bigNumber = 1234567890123L;

Floating-Point Types

Used to represent numbers with a decimal part.

  • float:

    • Size: 4 bytes (32 bits)
    • Precision: Single precision, approximately 6-7 decimal significant digits.
    • Usage: Used for scenarios requiring decimals but not high precision. Note: float values must have F or f at the end. For example: float price = 19.99F;
  • double:

    • Size: 8 bytes (64 bits)
    • Precision: Double precision, approximately 15 decimal significant digits.
    • Usage: The most commonly used floating-point type. Provides higher precision. By default, any decimal value you write (like 3.14) is treated as a double type.

Character Type

  • char:
    • Size: 2 bytes (16 bits)
    • Encoding: Unicode character.
    • Usage: Stores a single character. The value must be enclosed in single quotes ('). For example: char grade = 'A';, char chineseChar = '中';

Boolean Type

  • boolean:
    • Size: The JVM does not explicitly specify its size, usually considered to be 1 bit.
    • Values: Can only be true or false.
    • Usage: Used for logical judgments and conditional control, is the result of all relational operations.
java
public class DataTypesExample {
    public static void main(String[] args) {
        // Integer types
        int myAge = 30;
        long population = 8000000000L;

        // Floating-point types
        double pi = 3.1415926535;
        float temperature = 36.6F;

        // Character type
        char initial = 'J';

        // Boolean type
        boolean isLoggedIn = true;

        System.out.println("Age: " + myAge);
        System.out.println("World population: " + population);
        System.out.println("Pi: " + pi);
        System.out.println("Temperature: " + temperature);
        System.out.println("Initial: " + initial);
        System.out.println("Login status: " + isLoggedIn);
    }
}

2. Reference Data Types

Unlike primitive data types that directly store values, reference data types store a reference (which can be understood as a memory address) to an object somewhere in memory.

  • The variable itself does not store the object, but stores a "pointer" that points to that object.
  • All variables of types created with the new keyword are reference types.
  • If a reference variable does not point to any object, its value is null.

Common reference data types include:

  • Classes: All types defined by class, such as the Dog class we created earlier, and Java's built-in String class.
  • Interfaces: Types defined by interface.
  • Arrays: Collections used to store elements of the same type.
java
// String is a very commonly used reference type
String greeting = "Hello, Java!";

// Dog is a custom class we defined, myDog is a reference variable
Dog myDog = new Dog(); // myDog stores the address of the newly created Dog object

// Arrays are also reference types
int[] numbers = new int[5]; // numbers stores the address of the newly created array object

Primitive Types vs. Reference Types: Key Differences

FeaturePrimitive Data TypesReference Data Types
Storage ContentVariable directly stores the data valueVariable stores the memory address (reference) of the object
Storage LocationStack memoryThe object itself is in heap memory, the reference is in stack memory
Default ValueNumeric types are 0 or 0.0, boolean is false, char is \u0000null
SizeFixed size (e.g., int is 4 bytes)Variable size

Understanding the difference between these two data types is crucial for mastering Java's memory management and object operations.

Content is for learning and research only.