Core Java Programming: From Beginner to Project Development

Lesson 2.3: Non-Primitive Data Types in Java

Introduction

In the previous lesson, you learned about Primitive Data Types, which store simple values such as numbers, characters, and boolean values.

However, real-world applications often need to store more complex data, such as names, addresses, arrays, objects, and collections. Java provides Non-Primitive Data Types (also called Reference Data Types) for this purpose.

Unlike primitive data types, non-primitive data types can store multiple values, objects, or collections of data and provide many built-in methods for processing information.

In this lesson, you will learn what non-primitive data types are, their characteristics, and the commonly used non-primitive data types in Java.


Learning Objectives

After completing this lesson, you will be able to:

  • Understand non-primitive (reference) data types.

  • Differentiate between primitive and non-primitive data types.

  • Use String, Arrays, and Classes.

  • Understand object references.

  • Write programs using non-primitive data types.


What are Non-Primitive Data Types?

Non-Primitive Data Types are data types that store the reference (memory address) of an object instead of storing the actual value directly.

They are created by programmers or provided by Java libraries.

Examples include:

  • String

  • Arrays

  • Classes

  • Objects

  • Interfaces

  • Collections


Characteristics of Non-Primitive Data Types

  • Store references instead of actual values.

  • Can contain multiple values.

  • Have built-in methods.

  • Can be assigned null.

  • Usually have dynamic memory allocation.

  • Their size depends on the stored data.


Primitive vs Non-Primitive Data Types

Primitive Data Types Non-Primitive Data Types
Store actual values Store object references
Fixed size Size can vary
Built into Java Created by Java or programmers
Cannot be null Can be null
No methods Have methods and properties

1. String

A String is used to store text.

Strings are enclosed in double quotation marks.

Example:

String name = "Shiv Shankar"; System.out.println(name);

Output:

Shiv Shankar

Common String Methods

String name = "Java Programming"; System.out.println(name.length()); System.out.println(name.toUpperCase()); System.out.println(name.toLowerCase());


Output:

16
JAVA PROGRAMMING
java programming

2. Arrays

An Array stores multiple values of the same data type.

Example:

int[] marks = {85, 90, 78, 92}; System.out.println(marks[0]); System.out.println(marks[2]);

Output:

85
78

Arrays will be covered in detail in Module 6.


3. Classes

A Class is a blueprint for creating objects.

Example:

class Student { }

A class defines the properties and behaviors of an object.


4. Objects

An Object is an instance of a class.

Example:

Student s1 = new Student();

Here:

  • Student is the class.

  • s1 is the object.

  • new creates a new object.

Objects will be studied in detail in the Object-Oriented Programming module.


5. Interfaces

An Interface defines a set of methods that classes must implement.

Example:

interface Animal { void sound(); }

Interfaces help achieve abstraction and multiple inheritance.

You will learn about interfaces in the OOP module.


6. Collections

Collections are used to store groups of objects dynamically.

Example:

import java.util.ArrayList; ArrayList<String> students = new ArrayList<>(); students.add("Rahul"); students.add("Priya"); System.out.println(students);

Output:

[Rahul, Priya]

Collections will be discussed in a later module.


Example Program

public class NonPrimitiveExample { public static void main(String[] args) { String college = "Government Polytechnic"; int[] marks = {85, 90, 95}; System.out.println("College: " + college); System.out.println("First Mark: " + marks[0]); } }

Output:

College: Government Polytechnic
First Mark: 85

Understanding References

Consider the following code:

String city = "Ranchi";

The variable city does not store the text directly.

Instead, it stores a reference pointing to the memory location where "Ranchi" is stored.

This is why non-primitive data types are also called reference types.


Memory Representation

Primitive Variable:

age
 ↓
25

Non-Primitive Variable:

name
  ↓
Reference
  ↓
"Shiv Shankar"

Common Mistakes

Using Single Quotes for String

Incorrect:

String name = 'Rahul';

Correct:

String name = "Rahul";

Accessing an Invalid Array Index

Incorrect:

int[] marks = {10, 20};

System.out.println(marks[5]);

This causes an ArrayIndexOutOfBoundsException.


Forgetting the new Keyword

Incorrect:

Student s;

Correct:

Student s = new Student();

Best Practices

  • Use meaningful variable names.

  • Use String for text data.

  • Use arrays when storing multiple values of the same type.

  • Create objects only when required.

  • Follow Java naming conventions.


Key Points to Remember

  • Non-primitive data types store references.

  • They can store complex data.

  • String is the most commonly used non-primitive data type.

  • Arrays store multiple values.

  • Classes are blueprints for objects.

  • Objects are instances of classes.


Lesson Summary

In this lesson, you learned:

  • What non-primitive data types are.

  • The difference between primitive and non-primitive data types.

  • Common non-primitive data types such as String, Arrays, Classes, Objects, Interfaces, and Collections.

  • The concept of object references.

These concepts are the foundation of Object-Oriented Programming and advanced Java development.


Practice Questions

  1. What are non-primitive data types?

  2. What is the difference between primitive and non-primitive data types?

  3. What is a String?

  4. What is an Array?

  5. What is a Class?

  6. What is an Object?

  7. Why are non-primitive data types called reference types?

  8. Name any five non-primitive data types.


Programming Exercise

Write Java programs to:

  1. Store your name using a String.

  2. Create an array of five marks and display them.

  3. Create a simple Student class.

  4. Create an object of the Student class.

  5. Print the values stored in the String and array.


Assignment

Create a program named StudentData.java that:

  • Stores the student’s name using a String.

  • Stores five subject marks using an array.

  • Displays the student’s name and all marks.

  • Calculate and print the total marks using the array.

Next Lesson: Lesson 2.4 – Type Casting in Java

Scroll to Top