Java Primitive Types and Non-Primities (Reference) Types
Java Primitive Types and Non-Primities (Reference) Types
A data type is an attribute of a variable which tells the compiler or interpreter how the programmer
intends to use the variable. It defines the operations that can be done on the data and what type of values
can be stored. In this article, I will give you a brief insight into the different data types in Java. According
to the properties they possess, data types are divided into two groups:
Primitive Data Types: A primitive data type is pre-defined by the programming language. The size and
type of variable values are specified, and it has no additional methods.
Non-Primitive Data Types: These data types are not actually defined by the programming language but
are created by the programmer. They are also called “reference variables” or “object references” since they
reference a memory location which stores the data.
You can refer to the below figure to understand the different data types with respect to the memory
allocated to them.
Now let’s understand each of these data types in depth. First I will tell you what is boolean data type.
That was all about the boolean data type. I hope you understood it. Now let’s move further and
understand the next data type i.e. byte data type.
That was all about the boolean data type. I hope you understood it. Now let’s move further and
understand the next data type i.e. byte data type.
byte data type
This is an example of a primitive data type. It is an 8-bit signed two’s complement integer. It stores whole
numbers that lie between -128 to 127. A byte data type is helpful for saving memory in large amounts.
Now let’s write a small program and understand how it works.
That was all about the byte data type. Now let’s move further and comprehend the following data type
i.e. char.
That was all about the char data type. I hope you understood it. Now let’s move further and understand
the next data type on the list i.e. short data type.
For example:
For example:
That was all about the long data type. Now let’s move and see floating data types.
Floating Datatypes
You should use a floating point type whenever you need a number with a decimal, such as 8.88 or
3.14515.
So, this is how you can use the float data type. Now let’s see one more floating data type i.e. double.
That was all about Double data type and this brings us to the end of Primitive Datatypes. Now let’s figure
out the difference between primitive and non-primitive data types.
Non-Primitive Datatypes
Non-Primitive data types refer to objects and hence they are called reference types. Examples of non-
primitive types include Strings, Arrays, Classes, Interface, etc. Below image depicts various non-primitive
data types.
Strings: String is a sequence of characters. But in Java, a string is an object that represents a sequence
of characters. The java.lang.String class is used to create a string object. If you wish to know more about
Java Strings, you can refer to this article on Strings in Java.
Arrays: Arrays in Java are homogeneous data structures implemented in Java as objects. Arrays store
one or more values of a specific data type and provide indexed access to store the same. A specific
element in an array is accessed by its index. If you wish to learn Arrays in detail, then kindly check out
this article on Java Arrays.
Classes: A class in Java is a blueprint which includes all your data. A class contains fields(variables) and
methods to describe the behavior of an object.
Interface: Like a class, an interface can have methods and variables, but the methods declared
in interface are by default abstract (only method signature, no body).
So that was all about the non-primitive data types. Now let’s understand the difference between primitive
and non-primitive data types.
Primitive types are predefined in Java. Non-primitive types are created by the programmer and is
not defined by Java.
Non Primitive types can be used to call methods to perform certain operations, while primitive
types cannot.
A primitive type always has a value, whereas non-primitive types can be null.
A primitive type starts with a lowercase letter, while non-primitive types start with an uppercase
letter.
The size of a primitive type depends on the data type, while non-primitive types have all the
same size.