Non-Primitive Data Types in Java
In the previous blog, we learned about Data Types in Java. If you want to know more about it then visit Data Types in Java. In this blog, we will go through Non-Primitive Data types in Java. In Java Data Types are broadly divided into two categories:
- Primitive Data types
- Non-Primitive Data types
We have already learned about Primitive Data Types in our last blog. Now we will go through Non-Primitive Data Types.
Non-Primitive Data Types
Non-primitive data types are the data types that are created by programmers. They're not predefined in java like primitive data types. Non Primitive data types are used to store a group of values or several values. A variable of non-primitive data types references a memory location where data is stored within the heap memory. It references a memory where an object is really placed. Therefore non-primitive data types are called referenced data types. There are five non-primitive data types:
Class
class is created by a user(user-defined) in Java. The template or blueprint from which objects are made is called a class. When an object is created then it is said as an instance of the class. It represents the common attributes and functions of an object. class in Java is denoted by the keyword class.
e.g.
Object
Any real-world thing that has properties and actions is known as an object. It is an entity and it has behavior as well as state, where state represents properties and behavior represents actions or functionality.
e.g.
Output
String
String in Java stores a sequence of characters. It is a non-primitive data type, but it is predefined in Java. As we know that in C and C++ it is mandatory to add the null character at the end of the string. But in Java it is not required, there is no need to add null at the end of the string.
e.g.
Output
Array
Array in Java is used to store multiple variables of the same type. It is a non-primitive data type that stores multiple variables of the same type in a sequential manner. The declaration of an Array in Java is the same as we declare the normal variables in Java, but the difference is we have to add square brackets([ ]) along with the variable.
e.g.
Output
Interface
Interface in Java is the same as a class that has methods and variables but the difference is that all the methods in an interface are abstract by default. We can only define the methods in the interface. The implementation of the method is done in its child class.
e.g.
Output