Java Non Primitive Data Types

Java Non-Primitive Data Types

In Java, non-primitive data types (also called reference types) are used to store references (memory addresses) to objects rather than actual values.
They are more complex than primitive types and provide methods to perform operations.


🔹 Characteristics of Non-Primitive Types

Feature Description
Memory Stored in heap memory
Default Value null
Methods Have built-in methods (unlike primitives)
Examples String, Arrays, Classes, Objects, Interfaces

🔹 Common Non-Primitive Data Types

  1. String – Stores text

  2. Arrays – Stores multiple values in a single variable

  3. Classes & Objects – User-defined types

  4. Interfaces – Blueprint for classes


1️⃣ String Example

String name = "Vipul";
String greeting = "Hello, " + name + "!";

System.out.println(name);
System.out.println(greeting);
System.out.println("Length of name: " + name.length());

Output:

Vipul
Hello, Vipul!
Length of name: 5

2️⃣ Array Example

int[] numbers = {10, 20, 30, 40, 50};
System.out.println("First number: " + numbers[0]);
System.out.println("Array length: " + numbers.length);

Output:

First number: 10
Array length: 5

3️⃣ Class & Object Example

class Student {
String name;
int age;
}

public class Test {
public static void main(String[] args) {
Student s1 = new Student();
s1.name = "Alice";
s1.age = 20;

System.out.println(s1.name + " is " + s1.age + " years old.");
}
}

Output:

Alice is 20 years old.

4️⃣ Interface Example (Basic Concept)

interface Vehicle {
void start();
}

class Car implements Vehicle {
public void start() {
System.out.println("Car started");
}
}

public class TestInterface {
public static void main(String[] args) {
Car c = new Car();
c.start();
}
}

Output:

Car started

🔹 Key Points

  • Non-primitive types store references, not actual values.

  • Default value is null if not initialized.

  • Can call methods (like length(), toUpperCase()) on them.

  • Include Strings, Arrays, Classes, Objects, Interfaces.

CodeCapsule

Sanjit Sinha — Web Developer | PHP • Laravel • CodeIgniter • MySQL • Bootstrap Founder, CodeCapsule — Student projects & practical coding guides. Email: info@codecapsule.in • Website: CodeCapsule.in

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *