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 to. 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 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.
Difference between the primitive and object data types in Java:
Now let’s look at a program that demonstrates the difference between the primitive and object data types in Java.
PRIMITIVE DATA TYPES
Initially: x = 10, y = 10
After changing y to 30: x = 10, y = 30
**Only value of y is affected here because of Primitive Data Type
REFERENCE DATA TYPES
Array c: [10, 20, 30, 40]
Array d: [10, 20, 30, 40]
Modifying the value at index 1 to 50 in array d
Array c: [10, 50, 30, 40]
Array d: [10, 50, 30, 40]
**Here value of c is also affected because of Reference Data Type
Let’s look at the difference between the primitive and object data type in a tabular manner.
|Properties||Primitive data types||Objects|
|Origin||Pre-defined data types||User-defined data types|
|Stored structure||Stored in a stack||Reference variable is stored in stack and the original object is stored in heap|
|When copied||Two different variable is created along with different assignment(only values are same)||Two reference variable is created but both are pointing to same object on heap|
|When changes are made in the copied variable||Change does not reflect in the original ones.||Changes reflect in the original ones.|
|Default value||Primitive datatypes do not have null as default value||Default value for the reference variable is null|
|Example||byte, short, int, long, float, double, char, boolean||array, string class, interface etc.|
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Is an array a primitive type or an object in Java?
- Comparison of boolean data type in C++ and Java
- Difference between data type and data structure
- Collection Data Type in Apache Cassandra
- Type conversion in Java with Examples
- FileStore type() method in Java with Examples
- Difference between Type Casting and Type Conversion
- Difference between Data Scientist, Data Engineer, Data Analyst
- Static and Dynamic data structures in Java with Examples
- Type Erasure in Java
- Local Variable Type Inference or LVTI in Java 10
- Basic Type Base64 Encoding and Decoding in Java
- How to create a Java HashMap of user defined class type?
- Difference Between Data Mining and Data Analysis
- Difference Between Data Visualization and Data Analytics
- Difference between a Data Analyst and a Data Scientist
- Difference between Data Warehousing and Data Mining
- Difference between Data Warehouse and Data Mart
- Difference Between Data Science and Data Visualization
- Difference between Data Lake and Data Warehouse
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.