Primitive Data Types in Java

Primitive Data Types in Java

28 Mar 2024
Beginner
130 Views
20 min read
Learn via Video Course & by Doing Hands-on Labs

Java Programming For Beginners Free Course

Primitive Data Types in Java: An Overview

Primitive Data Types in Java is one of the two types of Data Types, that are Primitive and Non Primitive Data Types in Java, which hold the basic kinds of data in Java Programming Language. In this Java tutorial, we’ll try to understand What are Primitive Data Types in Java with Example Program and What are different types of Java Primitive Data Type. For more tips and guidance to learn Java Programming, try our 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.

1. Boolean Data Type

The Boolean Data Type is used to store only two values, that is, either ‘True’ or ‘False’. This Data Type is used in simple flags that track true/false 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 is used to save memory in large arrays where there is a need for memory saving. It is an 8-bit signed two’s complement integer. It has a default value of 0 and can range its value from -128 (minimum value) to 127 (maximum value).

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 Char Data Type is used to store characters like letters, digits, or special symbols. The character should be in single quotes. It is a single 16-bit Unicode character and can range from '\u0000' ,or 0 (minimum value) to '\uffff' (maximum value).

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

Just like the Byte Data Type, the Short Data Type is also helpful in saving memory in large arrays where there is a need for memory saving. It is a 16-bit signed two's complement integer. It also has a default value of 0 and can range from -32768 (minimum value) to 32767 (maximum value).

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 is generally used to store integer values. It is a 32-bit signed two’s complement integer. It also has a default value of 0 and can range from -2^31(minimum value) to 2^31-1(maximum value).

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 is usually helpful when there is a need for a wide range of values and Int Data Type is not large enough to store them all. Its default value is also 0 and can range from -2^63 (minimum value) to 2^63 -1 (maximum value).

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 is used to store fractional or decimal numbers. It is a single-precision 32-bit IEEE 754 floating point. It has a default value of 0.0F and its range is unlimited.

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 is also a data type that is used to store fractional or decimal numbers. It is a double-precision 64-bit IEEE 754 floating point. It has a default value of 0.0d and its range is also unlimited just like Float Data Type.

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
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 Programming Course.

FAQs

Q1. What is primitive data types in Java?

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

Q2. How many non primitive data types are there in Java?

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.

Q3. What is a primitive data type?

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.

Q4. What is an example of primitive?

An example of primitive data type in Java is the Int type. It is used to store integer values. For example:
int number = 10;
Share Article
Live Training Batches Schedule
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at Scholarhat by DotNetTricks)

Shailendra Chauhan is the Founder and CEO at ScholarHat by DotNetTricks which is a brand when it comes to e-Learning. He provides training and consultation over an array of technologies like Cloud, .NET, Angular, React, Node, Microservices, Containers and Mobile Apps development. He has been awarded Microsoft MVP 8th time in a row (2016-2023). He has changed many lives with his writings and unique training programs. He has a number of most sought-after books to his name which has helped job aspirants in cracking tough interviews with ease.
Self-paced Membership
  • 22+ Video Courses
  • 750+ Hands-On Labs
  • 300+ Quick Notes
  • 55+ Skill Tests
  • 45+ Interview Q&A Courses
  • 10+ Real-world Projects
  • Career Coaching Sessions
  • Email Support
Upto 60% OFF
Know More
Accept cookies & close this