Primitive and Non Primitive 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 different data types like integer number, float number, character etc. In java there are basically two types of data types :

  • Primitive Data Type
  • Non Primitive Data Type

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 values while data types float, double is also known as Floating point data type because they are used to store float type values.

How do you find the range of integer data types in java ?

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 in java because variables of primitive data types directly holds the values rather than the reference(address) of that value in memory. That is why when you access a variable of primitive type, you get the value directly rather than the reference of that value in memory.

Primitive data type program in Java

 class PrimitiveDataTypeExample {  
    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';
                 
       // Some incorrect declaration
       // long longVar2 = 34287; Must end with l or L
       // float floatVar2 = 123.45; Must end with f or F         
       
       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);   
    }  
 }   

Output:

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

Which data type is best for storing integer numbers ?

It's completely depend on integer number. As a good programming style always prefer lower size data type over higher size data type if the value that need to be stored comes within the range of that data type. For eg. any value from range -128 to 127 can be stored in short, byte, int, long data type but prefer short data type over others as it takes less space in memory.

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 basically the non-primitive data type. Non primitive data types are generally built using primitive data types, for example classes contains primitive variables.

Variables of non-primitive data type doesn't contain the value directly, instead they contain a reference(address) to an object in memory. That is why non primitive types are also called as reference types. If you access a non-primitive variable, it will return a reference not the value. The objects of non primitive type may contain any type of data, primitive or non-primitive. Except primitive data type everything is non-primitive data type in java.

If I create a class MyFirstProgram, would it be a non primitive data type ?

Yes, MyFirstProgram will be a non primitive data type.

Is string a primitive data type in java ?

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

Why String is not a primitive data type ?

Because String is a class defined in java language, it has it's own methods to manipulate and operate over object of String class. Every class in java, whether it is defined by java or by programmer is a non primitive data type.

Non primitive data type program in Java

 class NonPrimitiveDataTypeExample {  
    public static void main(String[] args) {                        
       // String is a non primitive data type define in Java
       String nonPrimStr = "String is a non primitive data type";
       System.out.println("nonPrimStr = "+ nonPrimStr);
       // Integer is a non primitive data type define in Java
       Integer intVal = new Integer(10);
       System.out.println("intVal = "+ intVal);
       // This class itself is a non primitive data type
       NonPrimitiveDataTypeExample np = new NonPrimitiveDataTypeExample(); 
       System.out.println("np = "+ np.toString());
    }  
 }   

Output:

nonPrimStr = String is a non primitive data type
intVal = 10
np = NonPrimitiveDataTypeExample@15db9742

Note : The value of np variable in last line of output could be different in your case as it depends on specific machine.

What are examples of java non primitive data types ?

Any class or interface created by you or already created in java are non primitive data types. Some of the examples of non primitive data type in java are String, StringBuilder, Arrays, Integer, Character etc.

Difference between primitive and non primitive data types in java

  • Primitive data types(refer the table above) are predefined by java itself while non primitive data types are basically the classes or interfaces defined by programmer or java itself.
  • Variable of primitive type holds the values directly, if you access a primitive type variable you get the value directly. Variables of non primitive types holds the references(address), if you access a non primitive type variable you get a reference.
  • Primitive data types are also known as value types since the variable of such types holds the values directly while non primitive data types are also known as reference types since variable of such types holds the references, not the values.
  • Every primitive type variable has a default value, if no value is assigned whereas a non primitive type variable points to null if no object is assigned.
  • Non primitive type variables can call methods of that type(class or interface) while it's not possible with primitive variables.
★★★
  • 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 can contain.
  • Primitive types are categorized as Integer, Floating point, characters and boolean.