Java Data Types

In Java, every value has a type. A data type tells the program what kind of data a variable can hold — such as a number, a character, or a true/false value. Java is a strongly typed language, meaning the data type of every variable must be declared before it is used.

Data types in Java are divided into two main categories: Primitive Data Types and Non-Primitive (Reference) Data Types.

1. Primitive Data Types

Primitive data types are the most basic building blocks of data in Java. There are 8 primitive data types in Java. Each one stores a specific kind of value and occupies a fixed amount of memory.

Overview Table

Data TypeSizeDefault ValueExample ValueDescription
byte1 byte0100Very small whole numbers
short2 bytes030000Small whole numbers
int4 bytes01500000Standard whole numbers
long8 bytes0L9876543210LLarge whole numbers
float4 bytes0.0f3.14fDecimal numbers (less precise)
double8 bytes0.0d3.14159265Decimal numbers (more precise)
char2 bytes'\u0000''A'A single character
boolean1 bitfalsetrue / falseTrue or false values only

byte

The byte data type stores very small whole numbers. Its range is from -128 to 127. It is useful when saving memory is important, such as in large arrays.

public class DataTypeDemo {
    public static void main(String[] args) {
        byte age = 25;
        System.out.println("Age: " + age);
    }
}

short

The short data type stores small whole numbers. Its range is from -32,768 to 32,767. It is rarely used in modern Java.

short temperature = -200;
System.out.println("Temperature: " + temperature);

int

The int data type is the most commonly used type for whole numbers. Its range is from -2,147,483,648 to 2,147,483,647. Unless there is a specific reason to use another integer type, int is the default choice.

int salary = 50000;
System.out.println("Salary: " + salary);

long

The long data type is used when numbers exceed the range of int. Long values must be followed by the letter L to distinguish them from int.

long worldPopulation = 8000000000L;
System.out.println("World Population: " + worldPopulation);

float

The float data type stores decimal numbers with up to 7 significant digits. Float values must be followed by the letter f.

float price = 19.99f;
System.out.println("Price: " + price);

double

The double data type is the default for decimal numbers and is more precise than float — it supports up to 15 significant digits. It does not need a suffix.

double piValue = 3.14159265358979;
System.out.println("Pi: " + piValue);

char

The char data type stores a single character. The value must be enclosed in single quotes.

char grade = 'A';
System.out.println("Grade: " + grade);

Java stores characters as Unicode values. So 'A' is actually stored as the number 65 internally.

boolean

The boolean data type holds only two possible values: true or false. It is commonly used in conditions and decision-making.

boolean isLoggedIn = true;
boolean hasPaid = false;
System.out.println("Logged in: " + isLoggedIn);
System.out.println("Has paid: " + hasPaid);

2. Non-Primitive (Reference) Data Types

Non-primitive data types are not predefined by Java — they are created by the programmer. They refer to objects stored in memory, which is why they are also called reference types.

Common non-primitive data types include:

  • String: A sequence of characters. Example: "Hello, Java!"
  • Arrays: A collection of values of the same type.
  • Classes: User-defined blueprints for creating objects.
  • Interfaces: Abstract contracts for classes to implement.
String name = "Alice";
System.out.println("Name: " + name);

Unlike primitive types, non-primitive types can be null — meaning they point to no object at all.

Difference Between Primitive and Non-Primitive

FeaturePrimitiveNon-Primitive
Defined byJava languageProgrammer or Java library
StoresActual valueReference (address) to an object
Default valueHas a default (e.g., 0, false)null
Examplesint, char, booleanString, Array, Class

Complete Example

public class AllDataTypes {
    public static void main(String[] args) {
        byte b = 10;
        short s = 500;
        int i = 75000;
        long l = 9000000000L;
        float f = 5.75f;
        double d = 19.99999;
        char c = 'J';
        boolean flag = true;
        String text = "Java is fun";

        System.out.println("byte: " + b);
        System.out.println("short: " + s);
        System.out.println("int: " + i);
        System.out.println("long: " + l);
        System.out.println("float: " + f);
        System.out.println("double: " + d);
        System.out.println("char: " + c);
        System.out.println("boolean: " + flag);
        System.out.println("String: " + text);
    }
}

Output:

byte: 10
short: 500
int: 75000
long: 9000000000
float: 5.75
double: 19.99999
char: J
boolean: true
String: Java is fun

Summary

  • Java has 8 primitive data types: byte, short, int, long, float, double, char, and boolean.
  • Use int for whole numbers and double for decimal numbers in most cases.
  • Use char for a single character and boolean for true/false values.
  • Non-primitive types include String, arrays, and classes — they store references to objects.
  • Java is strongly typed: every variable must have a declared type before use.

Leave a Comment

Your email address will not be published. Required fields are marked *