Java Data Types

    Data types specify the type of value that can be stored within the variable that may vary in size and value.

    Java supports two types of data types.

    Java Data Types

    The two types of Java data types are - primitive and non-primitive

    1. Primitive Data Types

    These data types are the building blocks for manipulating the data.

    Below is the list of the primitive data types available in Java:

    • Boolean
    • byte
    • char
    • short
    • int
    • long
    • float
    • double
    Data Type Data Size Description
    Boolean 1 byte Stores only two possible values - true or false. By default, its value is false.
    byte 2 byte Used to save memory in large arrays and can be used in place of int. The value range lies between -128 to 127, where -128 is the minimum value and the maximum value is 127. The default value is 0.
    char 1 byte It is a 16-bit Unicode character that ranges between '\u0000' (or 0) to '\uffff' (or 65,535) and is used to store characters.
    short 2 byte It is also used to save memory like a byte and also can be used in place of int. The value range lies between -32,768 to 32,767, where the minimum value is -32,768, and the maximum value is 32,767. The default value is 0.
    int 4 byte By default, int is used for integer value if there is no memory issue. The value range lies between - 2,147,483,648 to 2,147,483,647, where the minimum value is - 2,147,483,648, and the maximum value is 2,147,483,647. The default value is 0.
    long 8 byte It can be used when you need to store a large value range and is not possible with int. The value range lies between -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807, where the minimum value is 9,223,372,036,854,775,808, and the maximum value is 9,223,372,036,854,775,807. The default value is 0.
    float 4 byte It is a single-precision 32-bit data type with an unlimited value range. You can use it in large arrays to save memory.
    double 8 byte It is a double-precision 64-bit data type with an unlimited value range. You can use it for decimal values as a float data type.

    2. Non-primitive Data Types

    These data types refer to objects and are also known as reference types. The user creates these data types but not the programming language .

    Below is the list of non-primitive data types-

    • String
    • Array
    • Classes
    • Interfaces
    Data Type Description
    string It is an object that specifies the sequence of the characters. You can create a Java string using java.lang.String class.
    array It is a homogeneous data structure that can be implemented as objects. It contains values of the same data type and uses indexes to access the variable.
    classes It is a blueprint that contains all the data in it. You can declare variables, fields, or methods within a class.
    interfaces It is like a class containing variables and methods. But the methods have only a declaration but not the body. You have to implement the interface to define the methods.

    Check out: Array vs Pointer String