refresh java logo

Data Types in Java

A data is simply an instruction, it could be like 123, -125, 3.14, "hello" etc. A data type is basically classification of these data. These data can be divided in data types like integer number, float number, character etc. Java has mainly two types of data types, Primitive and Non-Primitive data type which is further divided into other data types.

Primitive Data Type

Primitive data type deals on basic data like 123, -125, 3.14 etc. Primitive data types are predefined by the java language itself. Considering size and type of data, java has defined eight types of primitive data type.

Type Size Range Default Value Example
byte 1 byte -128 to 127 0 byte b = 123;
short 2 byte -32768 to 32767 0 short s = 1234;
int 4 byte -231 to 231-1 0 int i = 123456;
long 8 byte -263 to 263-1 0L long l = 312456L;
long ll = 312456l;
float 4 byte 1.4E-45f to 3.4028235E38f 0.0f float f = 123.45f;
float ff = 123.45F;
double 8 byte 4.9E-324 to 1.7976931348623157E308 0.0d double d = 1234.67d;
double dd = 1234.67D;
char 2 byte '\u0000' (or 0) to '\uffff' (or 65,535 inclusive) '\u0000' char c = 'C';
boolean represents 1 bit of information Not Applicable false boolean b = true;

Data types byte, short, int, long is also known as Integer Data type because they can contain only integer type numbers while data types float, double is also known as Floating point data type because they are used to store float type numbers.

The range of integer data type values is calculated as -(2n-1) to (2n-1)-1, where n is the number of bits required. For example the byte data type requires 1 byte = 8 bits. Therefore the range of values that can be stored in byte data type is -(28-1) to (28-1)-1 = -27 to (27) -1 = -128 to 127

Primitive types are also called as value types because they directly contains the value of that type, which means accessing a primitive variable will give the value stored in that variable directly.

Which integer data type should I prefer to use if I need to store a integer numbers ?

It's completely depend on integer number, but as a good programming style you should give preference to lower size data type to store the value over higher size data type, if the value comes within the range of that data type. for eg. values range in -128 to 127 can be stored in short, byte, int, long but prefer short as it takes less space in memory over others.

Non-Primitive data type

Non-primitive data types are generally created by the programmer. In java every class or interface acts like a data type. Classes, interfaces, arrays etc defined by java or by programmer is the part of non-primitive data type. Non primitive data types are generally built using primitive data types like classes contains primitive variables.

Non-primitive data type variables doesn't contain the value directly, instead they contain a reference(address) to an object in memory. These objects may contain any type of data primitive or non-primitive. If you access a non-primitive variable, it will return a reference not the value. Except primitive data type everything is non-primitive data type.

If I create a class MyFirstProgram.java, is MyFirstProgram a Non primitive data type ?

Yes, MyFirstProgram will be a non primitive data type, the variables(objects) of this type can store the references.

Is String a primitive data type as it is defined by Java ?

No, String acts like a non primitive data type as it stores references.

Java Program of data Types

 class DataTypeExample
  {  
    public static void main(String[] args)
     {  
        byte byteVar = 123;  
        short shortVar = 1234;  
        int intVar = 123456;         
        long longVar = 3124567891L; // Must end with l or L
        float floatVar = 123.45f; // Must end with f or F
        double doubleVar = 12345.6789d; // d or D is optional 
        double doubleVar2 = 125.67;
        boolean booleanVar = true;   
        char charVar = 65; // code for A 
        char charVar2 = 'C';
                 
        // Incorrect declaration
        // long longVar2 = 34287; Must end with l or L
        // float floatVar2 = 123.45; Must end with f or F
         
        // dte is of non primitive type
        DataTypeExample dte = new DataTypeExample(); 
        
        System.out.println("byteVar = "+ byteVar);  
        System.out.println("shortVar = "+ shortVar); 
        System.out.println("intVar = "+ intVar); 
        System.out.println("longVar = "+ longVar); 
        System.out.println("floatVar = "+ floatVar);    
        System.out.println("doubleVar = "+ doubleVar);
        System.out.println("doubleVar2 = "+ doubleVar2);   
        System.out.println("booleanVar = "+ booleanVar);     
        System.out.println("charVar = "+ charVar);   
        System.out.println("charVar2 = "+ charVar2);   
        System.out.println("dte = "+ dte.toString());
     }  
  }   

Output:

byteVar = 123
shortVar = 1234
intVar = 123456
longVar = 3124567891
floatVar = 123.45
doubleVar = 12345.6789
doubleVar2 = 125.67
booleanVar = true
charVar = A
charVar2 = C
dte = DataTypeExample@1db9742

The value of dte in last line of output could be different in your case as it depends on system configurations.

★★★
  • Data types defined by programming language itself is called as built in data types.
  • All primitives data types are built-in data types but not all built-in data types are primitives like String, Arrays etc.
  • Every variable in a java program must have a data type, either primitive or non-primitive, that data type decides what type of data the variable may contain.
  • Primitive types are categorized as Integer, Floating point, characters and boolean.