0
0
Javaprogramming~5 mins

Primitive data types in Java

Choose your learning style9 modes available
Introduction

Primitive data types store simple values like numbers and letters. They are the basic building blocks for data in Java.

When you need to store whole numbers like age or count.
When you want to store decimal numbers like price or temperature.
When you need to store a single character like a letter or symbol.
When you want to store true or false values for conditions.
When you want to save memory by using simple data types.
Syntax
Java
public class PrimitiveDataTypes {
    byte aByte;       // 8-bit integer
    short aShort;     // 16-bit integer
    int anInt;        // 32-bit integer
    long aLong;       // 64-bit integer
    float aFloat;     // 32-bit decimal
    double aDouble;   // 64-bit decimal
    char aChar;       // 16-bit Unicode character
    boolean aBoolean; // true or false
}

Each primitive type has a fixed size and stores a simple value.

Primitive types are not objects and do not have methods.

Examples
Different integer types store numbers of different sizes. Use L suffix for long literals.
Java
byte smallNumber = 100;
short mediumNumber = 30000;
int largeNumber = 1000000;
long veryLargeNumber = 10000000000L;
Use f suffix for float literals. Double is more precise than float.
Java
float decimalNumber = 3.14f;
double preciseDecimal = 3.1415926535;
Characters are enclosed in single quotes. Boolean stores true or false.
Java
char letter = 'A';
boolean isJavaFun = true;
Zero is a valid integer. Boolean can only be true or false, never numbers.
Java
int zero = 0;
boolean isZero = false;
Sample Program

This program shows how to declare and use all primitive data types in Java. It prints their values to the screen.

Java
public class PrimitiveDataTypesDemo {
    public static void main(String[] args) {
        byte age = 25;
        short year = 2024;
        int population = 1000000;
        long distanceToMoon = 384400000L;
        float price = 19.99f;
        double pi = 3.1415926535;
        char grade = 'A';
        boolean isJavaFun = true;

        System.out.println("Age: " + age);
        System.out.println("Year: " + year);
        System.out.println("Population: " + population);
        System.out.println("Distance to Moon (meters): " + distanceToMoon);
        System.out.println("Price: $" + price);
        System.out.println("Pi: " + pi);
        System.out.println("Grade: " + grade);
        System.out.println("Is Java fun? " + isJavaFun);
    }
}
OutputSuccess
Important Notes

Time complexity is not applicable because these are simple data holders.

Primitive types use fixed memory sizes, so they are fast and efficient.

Common mistake: Using float without f suffix causes errors.

Use primitive types when you need simple values and want better performance than objects.

Summary

Primitive data types store simple values like numbers, characters, and true/false.

They have fixed sizes and are not objects.

Use the right type based on the size and kind of data you want to store.