DEV Community

Harini
Harini

Posted on

Primitive Data Types in Java (With Examples)

In Java, primitive data types are the most basic building blocks of data. They store simple values directly in memory and are not objects.

Java provides 8 primitive data types, categorized into:

  • Numeric Types
  • Character Type
  • Boolean Type

Let’s explore each one separately

1. byte

The byte data type is used to store small integer values. It saves memory in large arrays.

Size: 1 byte (8 bits)
Range: -128 to 127

Example:

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

2. short

The short data type is larger than byte but smaller than int.

Size: 2 bytes
Range: -32,768 to 32,767

Example:

public class ShortExample {
    public static void main(String[] args) {
        short temperature = 15000;
        System.out.println("Temperature: " + temperature);
    }
}
Enter fullscreen mode Exit fullscreen mode

3. int

The int data type is the most commonly used type for storing integers.

Size: 4 bytes
Range: -2,147,483,648 to 2,147,483,647

Example:

public class IntExample {
    public static void main(String[] args) {
        int salary = 50000;
        System.out.println("Salary: " + salary);
    }
}
Enter fullscreen mode Exit fullscreen mode

4. long

The long data type is used for very large integer values.

Size: 8 bytes
Range: Very large values

  • Must use L at the end

Example:

public class LongExample {
    public static void main(String[] args) {
        long population = 9876543210L;
        System.out.println("Population: " + population);
    }
}
Enter fullscreen mode Exit fullscreen mode

5. float

The float data type is used for decimal numbers (single precision).

Size: 4 bytes

  • Must use f at the end

Example:

public class FloatExample {
    public static void main(String[] args) {
        float price = 99.99f;
        System.out.println("Price: " + price);
    }
}
Enter fullscreen mode Exit fullscreen mode

6. double

The double data type is used for decimal numbers with higher precision.

Size: 8 bytes

  • Default type for decimal values

Example:

public class DoubleExample {
    public static void main(String[] args) {
        double pi = 3.1415926535;
        System.out.println("Value of Pi: " + pi);
    }
}
Enter fullscreen mode Exit fullscreen mode

7. char

The char data type is used to store a single character.

Size: 2 bytes
Uses Unicode values

Example:

public class CharExample {
    public static void main(String[] args) {
        char grade = 'A';
        System.out.println("Grade: " + grade);
    }
}
Enter fullscreen mode Exit fullscreen mode

8. boolean

The boolean data type stores only two values: true or false.

Size: 1 bit (logical)

Example:

public class BooleanExample {
    public static void main(String[] args) {
        boolean isJavaFun = true;
        System.out.println("Is Java Fun? " + isJavaFun);
    }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)