non primitive data types in java

2-Master Non Primitive data types the easy way

๐Ÿ“Œ Introduction

In the previous article, we discussed Primitive Data Types in Java, which store simple values directly. However, real-world applications require handling complex data, multiple values, text, and user-defined entities.

To handle such requirements, Java provides Non-Primitive Data Types, also known as Reference Data Types.


๐Ÿง  Primitive vs Non-Primitive Data Types (Quick Recall)

๐Ÿ“Š Key Difference
Primitive Data TypesNon-Primitive Data Types
Store simple valuesStore complex data
Fixed sizeSize varies
Store actual valueStore memory address (reference)
PredefinedPredefined + User-defined
Examples: int, charExamples: Array, String, Class

๐Ÿ‘‰ This article focuses only on Non-Primitive Data Types.


๐Ÿงฉ What Are Non-Primitive Data Types?

๐Ÿ“˜ Definition

Non-Primitive Data Types are data types that:

  • Store references to objects
  • Can store multiple values or complex structures
  • Are created using classes

These data types allow Java to model real-world entities efficiently.


๐Ÿ“‚ Types of Non-Primitive Data Types in Java

๐Ÿ“Œ Common Non-Primitive Data Types
  1. Arrays
  2. String
  3. Classes
  4. Interfaces
  5. Objects

(In this article, focus is on Arrays, String, and Class/Object basics, as per syllabus flow.)


๐Ÿ“ฆ 1. Arrays

๐Ÿ“Œ What Is an Array?

An array is a collection of elements of the same data type, stored in contiguous memory locations.

int[] marks = {85, 90, 78, 92};

๐Ÿง  Why Arrays Are Non-Primitive
  • An array stores multiple values
  • It stores a reference to memory
  • It is an object in Java

๐Ÿงช Example: Array Usage
int[] numbers = new int[3];
numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;

โš ๏ธ Common Array Mistake
int[] arr = new int[3];
arr[3] = 40;   // ERROR

โŒ Runtime Error: ArrayIndexOutOfBoundsException
โœ” Valid indexes: 0, 1, 2


๐Ÿ”ค 2. String Data Type (Very Important)

๐Ÿ“Œ What Is a String?

A String is a sequence of characters enclosed in double quotes.

String name = "Java";

๐Ÿง  Why String Is Non-Primitive
  • String is a class
  • It creates objects
  • It stores reference, not value

๐Ÿงช Two Ways to Create String
String s1 = "Java";
String s2 = new String("Java");

Both create strings, but memory handling is different.


โš ๏ธ Tricky Code Example (IMPORTANT)
String s1 = "Java";
String s2 = "Java";

System.out.println(s1 == s2);

โœ” Output:

true

๐Ÿง  Reason:

  • Both refer to the same object in String Constant Pool

โš ๏ธ Another Tricky Example
String s1 = new String("Java");
String s2 = new String("Java");

System.out.println(s1 == s2);

โœ” Output:

false

๐Ÿง  Reason:

  • new keyword creates separate objects

โœ… Correct Way to Compare Strings
System.out.println(s1.equals(s2));

โœ” Output:

true

โŒ Common String Mistake

Using == instead of equals() to compare content.


๐Ÿงฑ 3. Classes

๐Ÿ“Œ What Is a Class?

A class is a blueprint used to create objects.

class Student {
    int rollNo;
    String name;
}

๐Ÿง  Why Class Is Non-Primitive
  • Stores multiple variables
  • Can contain methods
  • Creates objects dynamically

๐Ÿง 4. Objects

๐Ÿ“Œ What Is an Object?

An object is an instance of a class.

Student s = new Student();

๐Ÿง  Object Stores
  • Data (variables)
  • Behavior (methods)
  • Reference to memory

โš ๏ธ Common Object Mistake
Student s;
s.rollNo = 10;   // ERROR

โŒ Compilation Error:

variable s might not have been initialized

โœ” Correct:

Student s = new Student();

๐Ÿง  Memory Behavior of Non-Primitive Types

๐Ÿ“Œ Important Concept
  • Variables store reference
  • Actual data stored in heap memory
  • Multiple references can point to same object

๐Ÿงช Tricky Example
int[] a = {1, 2, 3};
int[] b = a;

b[0] = 99;
System.out.println(a[0]);

โœ” Output:

99

๐Ÿง  Reason:

  • Both a and b point to the same array object

โš ๏ธ Common Mistakes with Non-Primitive Data Types

โŒ 1. Null Pointer Exception
String s = null;
System.out.println(s.length());

โŒ Runtime Error:

NullPointerException

โŒ 2. Using == Instead of equals()
  • Compares reference, not content

โŒ 3. Forgetting Object Creation

Declaring reference without creating object


โŒ 4. Assuming Arrays Auto-Resize

Java arrays have fixed size


โŒ 5. Confusing Default Values
  • Object reference โ†’ null
  • Not actual object

๐Ÿ“Š Summary Table: Non-Primitive Data Types

TypeDescription
ArrayStores multiple values of same type
StringStores sequence of characters
ClassBlueprint for objects
ObjectInstance of a class
InterfaceBlueprint for behavior

๐ŸŽฏ Why Non-Primitive Data Types Are Important

โญ Key Benefits
  • Store complex data
  • Support object-oriented programming
  • Enable real-world modeling
  • Improve code structure
  • Allow dynamic memory usage

๐Ÿ Conclusion

๐Ÿ“ Final Summary

Non-Primitive Data Types form the backbone of object-oriented programming in Java. They allow programs to handle complex data, multiple values, and real-world entities efficiently. Understanding their memory behavior, common mistakes, and comparison techniques is essential before moving to classes, objects, and OOP concepts.

A strong grasp of non-primitive data types prepares learners for:

  • Classes & Objects
  • Inheritance
  • Polymorphism
  • Data structures

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

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