Java Data Types

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

    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- 8 types of primitive data types:

    • Boolean data type
    • byte data type
    • char data type
    • short data type
    • int data type
    • long data type
    • float data type
    • double data type
    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. value-range lies between -128 to 127 where -128 and the maximum value is 127 and 0 is the default value.
    char 1 byte It is a 16-bit Unicode character which ranges between '\u0000' (or 0) to '\uffff' (or 65,535) and used to store characters.
    short 2 byte It is also used to save memory like byte and also can be used in place of int. value-range lies between -32,768 to 32,767 where the minimum value is -32,768 and the maximum value is 32,767 and 0 is the default value.
    int 4 byte By default, int is used for integer value if there is no memory issue. 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 and 0 is the default value.
    long 8 byte It can be used when you need to store a large value range and not possible with int. 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 and 0 is the default value.
    float 4 byte It is single-precision 32 bit with an unlimited value range. You can use it in large arrays to save memory. This data type cannot be used for precise data.
    double 8 byte It is a double-precision 64 bit with an unlimited value range. You can use it for decimal value as a float data type. This data type cannot be used for precise data.

    2. Non-primitive data types

    These data types refer to objects and are also known as reference types. These data types are created by the user but not by 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 declaration but not the body. You have to implement the interface to define the methods.

    For a brief understanding or comparison between array vs string visit our blog post .

    Conclusion

    A data type in computer programming specifies an attribute that tells the compiler or interpreter how a programmer is using that data. To put it simply, a data type specifies what kind of value a variable has. In general, there are two distinct data types in Java, namely primitive and non-primitive, which we have discussed in this article.

    People are also reading: