Non-Primitive Datatypes (Reference Types)

Introduction

In Java, data types are divided into two main categories:

  • Primitive Data Types – predefined and store simple values like int, float, boolean, etc.
  • Non-Primitive Data Types (Reference Types) – used to store objects and complex data structures.

Unlike primitive types, non-primitive data types refer to memory locations rather than storing values directly.

They are created by the user or Java API and include:

  • Strings
  • Arrays
  • Classes
  • Interfaces
  • Enums

1. Strings

A String in Java is a sequence of characters enclosed in double quotes. It is an object of the String class and is immutable (once created, it cannot be changed).

Example:

public class ShikshaSanchar {
    public static void main(String[] args) {
        String name = "ShikshaSanchar";
        System.out.println("Welcome to " + name);
        System.out.println("Length: " + name.length());
        System.out.println("Uppercase: " + name.toUpperCase());
    }
}

Output:

Welcome to ShikshaSanchar

Length: 14

Uppercase: SHIKSHASANCHAR

Key Points

  • Strings are stored in the String Pool for memory efficiency.
  • Common methods:
    • length() → returns number of characters.
    • toUpperCase() / toLowerCase() → changes case.
    • concat() → joins two strings.
    • equals() → compares two strings.

2. Arrays

An Array is a collection of elements of the same data type stored in contiguous memory. Arrays in Java are objects, meaning they belong to the reference data type category.

Example:

public class ShikshaSanchar {
    public static void main(String[] args) {
        int[] marks = {85, 90, 78, 92};
        System.out.println("Marks List:");
        for (int m : marks) {
            System.out.println(m);
        }
    }
}

Output:

Marks List:

85

90

78

92

Key Points

  • Array size is fixed once declared.
  • Supports both 1D and multi-dimensional arrays.
  • Common operations: traversing, updating, sorting.

Syntax:

int[] arr = new int[5];   // Declaration + memory allocation
arr[0] = 10;              // Initialization

3. Classes

A Class is a blueprint for creating objects. It defines attributes (variables) and behaviors (methods) that an object can have.

Example:

class Student {
    String name;
    int age;

    void display() {
        System.out.println("Name: " + name + ", Age: " + age);
    }
}

public class ShikshaSanchar {
    public static void main(String[] args) {
        Student s1 = new Student();   // Object creation
        s1.name = "Simran";
        s1.age = 21;
        s1.display();
    }
}

Output:

Name: Simran, Age: 21

Key Points

  • A class defines properties and methods.
  • Objects are created using the new keyword.
  • Example: Student s1 = new Student();
  • Promotes object-oriented programming concept like encapsulation and reusability.

4. Interfaces

An Interface in Java is a blueprint of a class that contains abstract methods (no body). It is used to achieve abstraction and multiple inheritance.

Example:

interface Animal {
    void sound();  // Abstract method
}

class Dog implements Animal {
    public void sound() {
        System.out.println("Dog barks");
    }
}

public class ShikshaSanchar {
    public static void main(String[] args) {
        Dog d = new Dog();
        d.sound();
    }
}

Output:

Dog barks

Key Points

  • All methods in interfaces are abstract by default.
  • A class implements an interface using the implements keyword.
  • One class can implement multiple interfaces.
  • Helps in achieving loose coupling and polymorphism.

5. Enums

An Enum (Enumeration) is a special Java type used to define a collection of constants. Enums make code more readable and type-safe.

Example

enum Day {
    MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}

public class ShikshaSanchar {
    public static void main(String[] args) {
        Day today = Day.FRIDAY;
        System.out.println("Today is " + today);
    }
}

Output:

Today is FRIDAY

Key Points

  • Enums are implicitly static and final.
  • Can have fields, methods, and constructors.
  • Commonly used in switch cases, configuration, and representing fixed sets of values.

Summary:

  • Non-primitive data types store references to objects, not actual values.
  • They support object-oriented features like encapsulation, inheritance, and polymorphism.
  • Used widely in real-world Java applications — from data structures to frameworks.
Concept Description Example
String Sequence of characters, immutable object. String name = "Java";
Array Collection of similar data types. int[] arr = {1,2,3};
Class Blueprint for objects. class Student { }
Interface Contains abstract methods, enables multiple inheritance. interface Animal { }
Enum Defines fixed constants. enum Days { MON, TUE }

Welcome to ShikshaSanchar!

ShikshaSanchar is a simple and helpful learning platform made for students who feel stressed by exams, assignments, or confusing topics. Here, you can study with clarity and confidence.

Here, learning is made simple. Notes are written in easy English, filled with clear theory, code examples, outputs, and real-life explanations — designed especially for students like you who want to understand, not just memorize.

Whether you’re from school, college, or someone learning out of curiosity — this site is for you. We’re here to help you in your exams, daily studies, and even to build a strong base for your future.

Each note on this platform is carefully prepared to suit all levels — beginner to advanced. You’ll find topics explained step by step, just like a good teacher would do in class. And the best part? You can study at your pace, anytime, anywhere.

Happy Learning! – Team ShikshaSanchar