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:
-
Studentis the class. -
s1is the object. -
newcreates 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
Stringfor 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.
-
Stringis 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
-
What are non-primitive data types?
-
What is the difference between primitive and non-primitive data types?
-
What is a String?
-
What is an Array?
-
What is a Class?
-
What is an Object?
-
Why are non-primitive data types called reference types?
-
Name any five non-primitive data types.
Programming Exercise
Write Java programs to:
-
Store your name using a
String. -
Create an array of five marks and display them.
-
Create a simple
Studentclass. -
Create an object of the
Studentclass. -
Print the values stored in the
Stringand 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