Primitive Data Types in Java

Primitive Data Types in Java

15 Jul 2025
Beginner
3.18K Views
25 min read
Learn with an interactive course and practical hands-on labs

Free Java Course With Certificate

Primitive data types in Java are the fundamental data types provided by the language to represent simple values directly in memory, such as integers, floating-point numbers, characters, and Boolean values.  They are not objects and are used to perform basic operations with high performance and minimal memory usage.

For more tips and guidance to learn Java Programming, try our Free Java Certification Course.

Java Primitive Data Types

In Java, Data Types usually specify what kind of data is stored in a variable. Java Primitive Data Types are the ones that are used to store the basic kinds of data which represent simple values such as Integers, Floating Numbers, Boolean, and Characters.

Read More -

1. Boolean Data Type

The Boolean data type in Java is a primitive data type that represents one of two possible logical values: true or false.
 It is primarily used to control the flow of a program through conditional statements such as if, while, and for, and to represent binary decisions or logical conditions.
Syntax
    boolean variableName;    

Example

Write the below code in the Java Compiler.
    public class BooleanExample {
    public static void main(String[] args) {
        boolean isJavaFun = true;
        boolean isCodingHard = false;

        System.out.println("Is Java fun? " + isJavaFun);
        System.out.println("Is coding hard? " + isCodingHard);

        if (isJavaFun) {
            System.out.println("Yes, Java is fun!");
        } else {
            System.out.println("No, Java is not fun.");
        }

        if (isCodingHard) {
            System.out.println("Coding is hard.");
        } else {
            System.out.println("Coding is not hard.");
        }
    }
}    

Output

Is Java fun? true
Is coding hard? false
Yes, Java is fun!    
Coding is not hard.

2. Byte Data Type

The byte data type in Java is an 8-bit signed two’s complement integer that ranges from -128 to 127. It is used to save memory in large arrays, especially when working with data where the memory footprint is critical. The default value of a byte variable is 0.
Syntax
    byte variableName;    

Example

    public class ByteExample {
    public static void main(String[] args) {
        byte numberOfStudents = 20;
        byte numberOfTeachers = 3;

        System.out.println("Number of students: " + numberOfStudents);
        System.out.println("Number of teachers: " + numberOfTeachers);

        byte totalPeople = (byte) (numberOfStudents + numberOfTeachers);
        System.out.println("Total number of people: " + totalPeople);

        // Byte data type ranges from -128 to 127
        byte valueOutOfRange = (byte) 150;
        System.out.println("Value out of range: " + valueOutOfRange);
    }
} 

Output

Number of students: 20
Number of teachers: 3     
Total number of people: 23
Value out of range: -106

3. Char Data Type

The byte data type in Java is an 8-bit signed two’s complement integer that ranges from -128 to 127. It is used to save memory in large arrays, especially when working with data where the memory footprint is critical. The default value of a byte variable is 0.

Syntax

    char variableName;    

Example

    public class CharExample {
    public static void main(String[] args) {
        char firstLetter = 'A';
        char secondLetter = 'B';

        System.out.println("First letter: " + firstLetter);
        System.out.println("Second letter: " + secondLetter);

        // Char can also hold Unicode characters
        char unicodeChar = '\u03A9'; // Greek capital letter omega
        System.out.println("Unicode character: " + unicodeChar);

        // Char can be used in arithmetic operations (as it's internally represented as
        // numbers)
        char result = (char) (firstLetter + 1);
        System.out.println("Next letter: " + result);
    }
} 

Output

First letter: A
Second letter: B    
Unicode character: Ω
Next letter: B

4. Short Data Type

The short data type in Java is a 16-bit signed two’s complement integer that ranges from -32,768 to 32,767. Like the byte data type, it is useful for saving memory in large arrays when memory optimization is required. The default value of a short variable is 0

Syntax

    short variableName;    

Example

    public class ShortExample {
    public static void main(String[] args) {
        short temperatureToday = 25;
        short numberOfStudents = 150;

        System.out.println("Temperature today: " + temperatureToday + " degrees Celsius");
        System.out.println("Number of students: " + numberOfStudents);

        short totalNumberOfPeople = (short) (numberOfStudents + 10);
        System.out.println("Total number of people: " + totalNumberOfPeople);

        // Short data type ranges from -32,768 to 32,767
        short valueOutOfRange = (short) 35000; // Corrected to use short and added casting
        System.out.println("Value out of range: " + valueOutOfRange);
    }
}    

On executing the above code in the Java Playground, we'll get the below output.

Output

Temperature today: 25 degrees Celsius
Number of students: 150
Total number of people: 160
Value out of range: -30536

5. Int Data Type

The int data type in Java is a 32-bit signed two’s complement integer that is commonly used to store whole numbers.
 It is the default data type for integer values in Java and offers a range from -2³¹ (−2,147,483,648) to 2³¹−1 (2,147,483,647). The default value of an int variable is 0.

Syntax

    int variableName;    

Example

    public class IntExample {
    public static void main(String[] args) {
        int numberOfStudents = 100;
        int numberOfTeachers = 10;

        System.out.println("Number of students: " + numberOfStudents);
        System.out.println("Number of teachers: " + numberOfTeachers);

        int totalPeople = numberOfStudents + numberOfTeachers;
        System.out.println("Total number of people: " + totalPeople);

        // Integers can hold very large values
        int bigValue = 2147483647;
        System.out.println("Big value: " + bigValue);

        // Integers overflow if the value exceeds the range
        int overflowValue = 2147483647 + 1;
        System.out.println("Overflow value: " + overflowValue);
    }
}    

Output

Number of students: 100
Number of teachers: 10     
Total number of people: 110
Big value: 2147483647      
Overflow value: -2147483648

6. Long Data Type

The long data type in Java is a 64-bit signed two’s complement integer used when a wider range than int is required. It is suitable for storing very large numeric values and has a range from −2⁶³ (−9,223,372,036,854,775,808) to 2⁶³−1 (9,223,372,036,854,775,807). The default value of a long variable is 0L, and long literals should be suffixed with an L or l.

Syntax

    long variableName;    

Example

We'll see the illustration in our Java Online Compiler.
    public class LongExample {
    public static void main(String[] args) {
        long populationOfCountry = 1500000000L; // Note the 'L' suffix for long literals
        long distanceToMoon = 384400000L;

        System.out.println("Population of the country: " + populationOfCountry);
        System.out.println("Distance to the moon (in meters): " + distanceToMoon);

        long totalDistance = populationOfCountry * distanceToMoon;
        System.out.println("Total distance (population x distance to moon): " + totalDistance);

        // Long data type can hold even larger values
        long bigValue = 9223372036854775807L;
        System.out.println("Big value: " + bigValue);

        // Long data type can also represent negative values
        long negativeValue = -1000000000L;
        System.out.println("Negative value: " + negativeValue);
    }
}    

Output

Population of the country: 1500000000
Distance to the moon (in meters): 384400000
Total distance (population x distance to moon): 576600000000000000
Big value: 9223372036854775807
Negative value: -1000000000

7. Float Data Type

The float data type in Java is a 32-bit single-precision IEEE 754 floating-point number used to store decimal or fractional values. It is suitable for saving memory in large arrays of floating-point numbers where precision is not a major concern. The default value of a float variable is 0.0f, and float literals must be suffixed with an f or F.

Syntax

    float variableName;   

Example

    public class FloatExample {
    public static void main(String[] args) {
        float temperatureToday = 25.5f; // Note the 'f' suffix for float literals
        float distanceToDestination = 123.45f;

        System.out.println("Temperature today: " + temperatureToday + " degrees Celsius");
        System.out.println("Distance to destination: " + distanceToDestination + " kilometers");

        float totalDistance = temperatureToday + distanceToDestination;
        System.out.println("Total distance: " + totalDistance + " units");

        // Float data type can represent very large or very small values
        float bigValue = 3.4028235E38f; // Maximum positive value for float
        float smallValue = 1.4E-45f; // Minimum positive value for float
        System.out.println("Maximum positive value for float: " + bigValue);
        System.out.println("Minimum positive value for float: " + smallValue);
    }
}    

Output

Temperature today: 25.5 degrees Celsius
Distance to destination: 123.45 kilometers    
Total distance: 148.95 units
Maximum positive value for float: 3.4028235E38
Minimum positive value for float: 1.4E-45

8. Double Data Type

The double data type in Java is a 64-bit double-precision IEEE 754 floating-point number used to store decimal or fractional values with greater precision than float.
It is the default data type for decimal values in Java and provides approximately 15–16 digits of precision. The default value of a double variable is 0.0d. It is commonly used for high-accuracy calculations in scientific and mathematical operations.

Syntax

    double variableName;    

Example

Let's see the demonstration in the Java Online Editor.
    public class DoubleExample {
    public static void main(String[] args) {
        double temperatureToday = 25.5;
        double distanceToDestination = 123.45;

        System.out.println("Temperature today: " + temperatureToday + " degrees Celsius");
        System.out.println("Distance to destination: " + distanceToDestination + " kilometers");

        double totalDistance = temperatureToday + distanceToDestination;
        System.out.println("Total distance: " + totalDistance + " units");

        // Double data type can represent very large or very small values
        double bigValue = 1.7976931348623157E308; // Maximum positive value for double
        double smallValue = 4.9E-324; // Minimum positive value for double
        System.out.println("Maximum positive value for double: " + bigValue);
        System.out.println("Minimum positive value for double: " + smallValue);
    }
}    

Output

Temperature today: 25.5 degrees Celsius
Distance to destination: 123.45 kilometers
Total distance: 148.95 units
Maximum positive value for double: 1.7976931348623157E308
Minimum positive value for double: 4.9E-324
Importance of Primitive Data Types

Primitive data types are fundamental to Java programming. They provide the most basic forms of data representation and are essential for writing efficient, clear, and high-performance code. Below are the key reasons why primitive data types are important:

 1. Basic Building Blocks of Data- Primitive data types serve as the foundation for data manipulation in Java. They allow developers to represent simple values such as numbers, characters, and logical states directly in memory.

 2. High Performance and Speed- Operations involving primitive types are executed faster than those involving objects. Since they store values directly in memory (rather than references), they eliminate the overhead associated with object creation and access.

3. Memory Efficiency- Primitive types are memory-efficient, especially when used in large arrays or memory-critical applications. For example, using byte or short instead of int can reduce memory usage significantly in certain scenarios.

4. Simplified Programming- Primitive data types make coding simpler and more straightforward. They are easy to declare and use, making them ideal for performing basic arithmetic, logical, and control flow operations.

 5. Essential for Control Structures- Control statements in Java—such as if, while, for, and switch—rely heavily on primitive types (especially boolean) to evaluate conditions and manage program flow.

6. Default Initialization- Each primitive type in Java has a default value (e.g., 0 for int, false for boolean), which ensures predictable behavior when variables are declared but not explicitly initialized.

 7. Avoids Object Overhead- Unlike wrapper classes (e.g., Integer, Double), primitive types do not include additional methods or metadata, making them lightweight and optimal for performance-sensitive tasks.

  List of All 8 Primitive Data Types with a Table:

Data typeSizeDefault valueRangeUsage example
byte1 byte0-128 to 127Memory-efficient arrays
short2 bytes0-32,768 to 32,767Small integers
int 4 bytes0  -2,147,483,648 to 2,147,483,647Default for integers
long8 bytes0L-9 quintillion to +9 quintillionLarge numeric values
float4 bytes0.0fApprox. ±3.4e38Low-precision decimals
double8 bytes0.0dApprox. ±1.8e308High-precision decimals
char2 bytes'\u0000'0 to 65,535 (Unicode characters)Single characters
Boolean1 bytefalsetrue or falseLogic/conditions

Summary

In this tutorial, we got to explore different Types of Primitive Data Types in Java and what are the use of each of the Data Types with proper Example Programs. Data Types are the basic topics you need to know if you are starting to learn Java. It will surely help you get a start to your Java Programming Journey. And we would be glad if you start with us under proper guidance through our Java Online Course Free With Certificate.

FAQs

Primitive Data Types in Java are the data type which hold basic values such as integer, floating numbers, character, and boolean.

Non Primitive Data Types, also known as Reference Types in Java, are mainly divided into 5 types , that are, arrays, strings, class, object, and interfaces.

In Java Programming Language, a primitive data type is a predefined data type that hold basic data values and are directly stored into the memory.

An example of primitive data type in Java is the Int type. It is used to store integer values. For example:
int number = 10;

Take our Java skill challenge to evaluate yourself!

In less than 5 minutes, with our skill challenge, you can identify your knowledge gaps and strengths in a given skill.

GET FREE CHALLENGE

Share Article
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at ScholarHat)

Shailendra Chauhan, Founder and CEO of ScholarHat by DotNetTricks, is a renowned expert in System Design, Software Architecture, Azure Cloud, .NET, Angular, React, Node.js, Microservices, DevOps, and Cross-Platform Mobile App Development. His skill set extends into emerging fields like Data Science, Python, Azure AI/ML, and Generative AI, making him a well-rounded expert who bridges traditional development frameworks with cutting-edge advancements. Recognized as a Microsoft Most Valuable Professional (MVP) for an impressive 9 consecutive years (2016–2024), he has consistently demonstrated excellence in delivering impactful solutions and inspiring learners.

Shailendra’s unique, hands-on training programs and bestselling books have empowered thousands of professionals to excel in their careers and crack tough interviews. A visionary leader, he continues to revolutionize technology education with his innovative approach.
Live Training - Book Free Demo
Azure AI & Gen AI Engineer Certification Training Program
28 Aug
08:30PM - 10:30PM IST
Checkmark Icon
Get Job-Ready
Certification
.NET Solution Architect Certification Training
30 Aug
10:00AM - 12:00PM IST
Checkmark Icon
Get Job-Ready
Certification
Software Architecture and Design Training
30 Aug
10:00AM - 12:00PM IST
Checkmark Icon
Get Job-Ready
Certification
Advanced Full-Stack .NET Developer with Gen AI Certification Training
31 Aug
08:30PM - 10:30PM IST
Checkmark Icon
Get Job-Ready
Certification
ASP.NET Core Certification Training
31 Aug
08:30PM - 10:30PM IST
Checkmark Icon
Get Job-Ready
Certification
Accept cookies & close this