Primitive data type vs. Object data type in Java with Examples
Categories: Java 8(JDK1.8)
Primitive Data Type: In Java, the primitive data types are the predefined data types of Java. They specify the size and type of any standard values. Java has 8 primitive data types namely byte, short, int, long, float, double, char and boolean. When a primitive data type is stored, it is the stack that the values will be assigned. When a variable is copied then another copy of the variable is created and changes made to the copied variable will not reflect changes in the original variable. Here is a Java program to demonstrate all the primitive data types in Java.
Object Data Type: These are also referred to as Non-primitive or Reference Data Type. They are so-called because they refer to any particular objects. Unlike the primitive data types, the non-primitive ones are created by the users in Java. Examples include arrays, strings, classes, interfaces etc. When the reference variables will be stored, the variable will be stored in the stack and the original object will be stored in the heap. In Object data type although two copies will be created they both will point to the same variable in the heap, hence changes made to any variable will reflect the change in both the variables. Here is a Java program to demonstrate arrays(an object data type) in Java.
PropertiesPrimitive data typesObjects
OriginPre-defined data typesUser-defined data types
Stored structureStored in a stackReference variable is stored in stack and the original object is stored in heap
When copiedTwo different variables is created along with different assignment(only values are same)Two reference variable is created but both are pointing to the same object on the heap
When changes are made in the copied variableChange does not reflect in the original ones.Changes reflected in the original ones.
Default valuePrimitive datatypes do not have null as default valueThe default value for the reference variable is null
Examplebyte, short, int, long, float, double, char, booleanarray, string class, interface etc.