top of page

Primitive and NON-Primitive Data Types and its importance in JAVA

Data types define the behavior of data like the way values of that type can be stored and operations that can be done on data. Java supports two kinds of data types:

1. Primitive data type

2. non-primitive or reference data type.

Importance of data types in Java

Data types are very significant in Java since it is a strongly typed language. In other words, they are attributes of data that inform the interpreter or compiler how the programmer intends to use the data. It implies that every operation is type-checked by the compiler to ensure type compatibility. So, illegal operations would not be compiled.

Strong type checking avoids errors and improves reliability. All values, expressions, and variables have a type to allow strong type checking. For instance, Java has no concept like a “type-less” variable. Whether you are using primitive or non-primitive data types, the type of a value decides what operations are permitted on it. Specifically, an operation permitted on one data type may not be permitted on another. Let's see the difference between Primitive and NON-Primitive.

Difference Between Primitive data types and non-primitive data types in JAVA

let’s Understand what primitive and non-primitive data types is in java,

1. Primitive data types:

Primitive data types are predefined (already defined) in Java. Primitive data types - includes byte, short, int, long, float, double, boolean and char.

2. Non-Primitive data types:

Non primitive data types are called reference types in Java, and they refer to an object. They are created by the programmer and are not defined by Java like primitives are. A reference type references a memory location where the data is stored rather than directly containing a value. Non-primitive data types in Java- Class, Object, String, Array, Interface

We all have that confusion whether “string” is a primitive data type or non-primitive data type in Java? So, to be clear string data type is a non-primitive data type, but it is predefined in java, some people also call it as a primitive data type since it solves the case where a char cannot store multiple characters, a string data type is used to store the sequence of characters. Let's see an example of how data stored in memory.

Figure :1

Figure :2

Figure 1 & 2 shows How primitive and non-primitive data types are stored in stack and Heap memory

In the above Example it clearly says that Primitive data type stores initialized value of a and b whereas for non-primitive data types it stores the reference value of the variable in the stack memory.

The difference between primitive and non-primitive data types are as follows:

Primitive data structure

Non-primitive data structure

Primitive data structure is a kind of data structure that stores the data of only one type.

Non-primitive data structure is a type of data structure that can store the data of more than one type.

Examples of primitive data structure are integer, character, float.

Examples of non-primitive data structure are Array, Linked list, stack.

Primitive data structure will contain some value, i.e., it cannot be NULL.

Non-primitive data structure can consist of a NULL value.

The size depends on the type of the data structure.

In case of non-primitive data structure, size is not fixed.

It starts with a lowercase character eg: int

It starts with an uppercase character eg: String

Primitive data structure can be used to call the methods.

Non-primitive data structure cannot be used to call the methods.

This brings us to the end of the blog on different Data types in Java. I hope you found it informative. Happy Learning!

296 views0 comments

Recent Posts

See All
bottom of page