Java is a versatile programming language that supports various data types. In Java data types are classified into two types: primitive and non-primitive data types. Primitive data types include int, char, boolean etc. Non-primitive data types encompass classes, arrays, interfaces, and more. In this article, we will discuss non primitive data types in Java, providing examples to illustrate their usage and importance.
What is Data Type?
In Java language, a data type is a classification that specifies which type of value a variable can hold and what operations can be performed on that value. Java is a statically typed language, which means that the data type of a variable must be specified at the time of declaration.

POSTGRADUATE PROGRAM IN
Multi Cloud Architecture & DevOps
Master cloud architecture, DevOps practices, and automation to build scalable, resilient systems.
Types of Data Types
In Programming language, Data types are categories used to define the type of data that a variable can hold. Data Types are generally divided into two main categories. primitive data types and non-primitive data types.
Primitive Data Types
Primitive data types are the most basic data types provided by a programming language. They are predefined in every programming language. The exact types and their names can vary between languages, But some common primitive data types here:
- Integer: It is used to represent whole numbers. Example ‘int’.
- Floating Point (float, double): This type represents a floating component. Examples include ‘float’ and ‘double’ in Java.
- Character (char): The represent a single character, examples include ‘char’ in Java.
- Boolean (boolean): This represents a value that can be either true or false.
- Byte: It represents an 8 bit signed integer.
- Short : They represent a 16 bit signed integer.
- Long: This type represents a 64 bit signed integer.
- Double: This type represents a double precision 64 bit IEEE 754 floating point.
Non-Primitive Data Types
The non-primitive data types (also called reference types) are more complex and are derived from primitive data types. They can store multiple values and provide more functionality. Non-primitive data types include. Here, we are discussing some common non primitive data types:
- Arrays: Used to store multiple values of the same type in a single variable. Example ‘int[]’ in Java.
- Strings: Used to represent sequences of characters. Example ‘String’ in Java.
- Classes: Used to define ‘interface’ in Java.
- objects that encapsulate data and functions. Example custom class definitions in Java.
- Interfaces: This defines methods that can be implemented by classes. Example Enumeration: Used to define a set of named constants. Example ‘enum’ in Java language.
- Collections: This type is used to store groups of objects, such as lists, sets and maps , Example ArrayList, and HashSet and HashMap in Java language.
Types of Non-Primitive Data Types in Java
- Classes and Object: Classes are the user defined data type that is used to create an object . A class contains a set of methods and instance variables that are common in Java language.
The following program demonstrates the classes and objects:
Program
class Example1{
int num = 100 ;
public Example1(){
System.out.println(num) ;
}
public void addition(int a, int b){
int c = a+b ;
System.out.println("Addition of Two numbers = "+c) ;
}
public static void substract(int a, int b){
int c = a-b ;
System.out.println("Subtraction of numbers"+c) ;
}
public static void main(String args[]){
Example1 ob = new Example1() ;
ob.addition(30, 50);
ob.substract(50, 30);
}
}
Output
100
Addition of Two numbers = 80
Subtraction of numbers =20
- String: Strings are an integral part of Java programming language. In Java, Strings are objects that represent sequences of characters. The ‘java.lang.String’ class is used to create and manipulate Strings.
Syntax for declaring Strings
Strings string_variable_name = new String(“<sequence_of_character>”)
Example 1: Standard String Declaration
The following example declares a string by using the non primitive data type strings and initializing them with some values.
Program
public class Example2 {
public static void main(String args[]){
String str1 = "Non-Primitive" ;
String str2 = "Data Type" ;
System.out.println(str1+" "+str2) ;
}
}
Output
Non-Primitive Data Type
Example 2: String Declaration Using new Operator
We can also define the string using the new operator in the Java language where the sequence of characters is passed as a single parameter to the new String() constructor.
The following program demonstrates the new Operator
Program
public class StringConstructor {
public static void main(String[] args) {
String str1 = new String("Computer working days");
System.out.print(str1);
}
}
Output
Computer working days
- Array: Arrays are non primitive data types in Java language. This is used for storing elements of the same data type in a contiguous manner in the computer system. It is the simplest and most widely used data structure in computer science and programming. They provide a way to store multiple items of the same type together.
Syntax of Array Declaration
<data_type> [] <array_name> = new <data_type> [size] ;
<data_type> <array_name> [] = {array_item_values} ;
int []arr = new int[5] ;
int arr[] = {1,2,3,4,5,6,8}
The following program demonstrates the array:
Program
import java.util.*;
public class Array2 {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
// Declaration & Initialization
int arr1[] = { 1, 2, 3, 4, 5,6,7,8,9,10 };
// Array of size 5 declared
int[] arr2 = new int[5];
for (int i = 0; i < 5; i++) {
arr2[i] = scn.nextInt();
}
scn.close();
System.out.print("Array arr1 elements: ");
for (int i = 0; i < 5; i++) {
System.out.print(arr1[i] + " ");
}
System.out.println();
System.out.print("Array arr2 elements: ");
for (int i = 0; i < 5; i++) {
System.out.print(arr2[i] + " ");
}
}
}
Output
Enter the Number from User
30
40
50
60
70
Array arr1 elements: 1 2 3 4 5
Array arr2 elements: 30 40 50 60 70
- Interface: An interface in Java is a reference type, similar to a class, that can contain only constants, method signatures, default methods, static methods, and nested types. Interfaces cannot contain instance fields or constructors. They are used to achieve abstraction and multiple inheritance in Java language.
The following program demonstrates the interface in Java language:
Program
// Define an interface
interface Animal {
void makeSound();
void eat();
}
// Implement the interface in a class
class Dog implements Animal {
public void makeSound() {
System.out.println("Woof");
}
public void eat() {
System.out.println("Dog is eating");
}
}
class Cat implements Animal {
public void makeSound() {
System.out.println("Meow");
}
public void eat() {
System.out.println("Cat is eating");
}
}
public class InterfaceExample {
public static void main(String[] args) {
// Create instances of Dog and Cat
Animal myDog = new Dog();
Animal myCat = new Cat();
// Call methods
myDog.makeSound();
myDog.eat();
myCat.makeSound();
myCat.eat();
}
}
Output
Woof
Dog is eating
Meow
Cat is eating
Difference between Primitive and Non-Primitive Data Types in Java
This is a comparison between primitive and non-primitive data types in Java in a tabular form.
| Aspect | Primitive Data Types | Non-Primitive Data Types |
| Definition | Basic Data Types predefined by the language | This data type created by the developer |
| Examples | Int,char, boolean, byte, sort, long, float, double | String, arrays, classes, interface |
| Memory Allocation | Allocated on the stack | Allocated on the heap |
| Size | It has fixed size (depends on the type) | Variable size(depends on the size) |
| Default Values | Default values (eg 0 for int, false, for boolean) | NULL (for objects) |
| Operations | It can be used with arithmetic and bitwise operators directly | This require methods or specific operations to manipulate |
| Storage | Stores actual values | Stores references to objects |

82.9%
of professionals don't believe their degree can help them get ahead at work.
Conclusion
In this article, we learned about the Non-primitive data types in Java language. These types allow for encapsulation of data and behavior, promoting code reusability and modularity. Understanding and effectively utilizing non-primitive data types is crucial for master Java programming. They provide the tools necessary for mastering Java programming, as they provide the tools necessary for creating sophisticated and efficient software solutions.
What are non-primitive data types in Java?
How are arrays considered non-primitive data types?
What are enums used for in Java language?
Why is it important to understand non-primitive data types in Java language?
Updated on August 12, 2024
