🎯 Java Object Arrays - Detailed Reference Cards

1
Array Declaration & Indexing
The Problem
❌ WRONG:
Object obj = new Object[3];
obj[0] = "hi"; // ERROR: array required, but Object found
Even though new Object[3] creates an array, the reference type is Object, not Object[]. You cannot index a plain Object reference.
The Solution
βœ… CORRECT:
Object[] obj = new Object[3];
obj[0] = "hi"; // Works perfectly
obj[1] = "bye";
obj[2] = 10;
Alternative: Casting
Object obj = new Object[3];
Object[] arr = (Object[]) obj; // Cast to array type
arr[0] = "hi"; // Now works
Rule: Only array type references (Type[]) support [] indexing. Plain object references cannot be indexed, even if they point to an array.
2
Arrays for Any Class Type
Universal Principle
You can create an array of ANY type in Java - built-in classes, wrapper classes, or your own custom classes.
Examples
// Built-in classes
String[] strings = new String[5];
Integer[] numbers = new Integer[10];

// User-defined classes
TestingClass[] test = new TestingClass[3];
Employee[] employees = new Employee[100];
Critical Understanding
TestingClass[] arr = new TestingClass[3];
// arr[0] = null
// arr[1] = null
// arr[2] = null

// Must create objects separately:
arr[0] = new TestingClass();
arr[1] = new TestingClass();
arr[2] = new TestingClass();
⚠️ Common Mistake: Creating an array does NOT create the objects! It only creates space to hold references. All elements start as null.
What Happens
arr[0].someMethod(); // NullPointerException!

// First create object, then use:
arr[0] = new TestingClass();
arr[0].someMethod(); // Now works
3
Polymorphism & Inheritance
Parent Array, Child Objects βœ…
class TestingClass {}
class SubTestingClass extends TestingClass {}

TestingClass[] arr = new TestingClass[3];
arr[0] = new TestingClass(); // βœ… Parent
arr[1] = new SubTestingClass(); // βœ… Child allowed!
Why it works: SubTestingClass IS-A TestingClass due to inheritance. A child can always be stored where parent is expected (polymorphism).
Child Array, Parent Objects ❌
SubTestingClass[] arr = new SubTestingClass[3];
arr[0] = new TestingClass(); // ❌ ArrayStoreException!
Runtime Error: The array is typed as SubTestingClass[]. You cannot put a parent object into a child array - it doesn't fit!
Array Covariance
// This is also legal:
TestingClass[] arr = new SubTestingClass[3];
arr[0] = new SubTestingClass(); // βœ… Works
arr[1] = new TestingClass(); // ❌ Runtime error!
Even if reference is TestingClass[], if the actual array is SubTestingClass[], you can only store SubTestingClass objects (or its subclasses).
πŸ’‘ Memory Trick: "Kids can sit in parent's chair (bigger space), but parent cannot squeeze into kid's chair (too small)."
4
Object[] - The Universal Array
Why Object[] is Special
In Java, every class inherits from Object (directly or indirectly). This means Object[] can hold ANY object type!
Examples of What Works
Object[] arr = new Object[5];
arr[0] = "Hello"; // String βœ…
arr[1] = 10; // Integer (autoboxed) βœ…
arr[2] = 3.14f; // Float (autoboxed) βœ…
arr[3] = new TestingClass(); // Custom class βœ…
arr[4] = new int[]{1,2,3}; // Even arrays! βœ…
Comparison Table
TestingClass[] Only TestingClass + subclasses
String[] Only String objects
Integer[] Only Integer objects
Object[] ANY object type!
Casting Caution
Object[] arr = new Object[3];
arr[0] = "Hello";
arr[1] = 10;

String s = (String) arr[0]; // βœ… OK
String x = (String) arr[1]; // ❌ ClassCastException!
When retrieving from Object[], you must cast back to the correct type. Wrong casting causes ClassCastException at runtime.
Key Rule: Object[] is the ONLY array type that accepts any object. All other arrays are type-restricted.
5
Autoboxing & Type Safety
What is Autoboxing?
Java automatically converts primitives to their wrapper classes when an object is required. This is called autoboxing.
// What you write:
arr[0] = 10;

// What Java does:
arr[0] = Integer.valueOf(10);
When Autoboxing Happens
βœ… Autoboxing occurs:
Integer x = 10; // int β†’ Integer
Object[] arr = new Object[3];
arr[0] = 10; // int β†’ Integer
list.add(10); // int β†’ Integer (ArrayList)

❌ No autoboxing:
int a = 10; // stays primitive
int b = a + 5; // primitive arithmetic
void method(int x) {} // primitive parameter
Autoboxing with Object[] βœ…
Object[] arr = new Object[3];
arr[0] = 10; // βœ… int β†’ Integer, works!
arr[1] = 3.14f; // βœ… float β†’ Float, works!
arr[2] = true; // βœ… boolean β†’ Boolean, works!
Works because Integer, Float, Boolean all extend Object.
Autoboxing with Typed Arrays ❌
TestingClass[] arr = new TestingClass[3];
arr[0] = 10; // ❌ ERROR!

// Java tries: arr[0] = Integer.valueOf(10);
// But Integer is NOT a TestingClass!
Critical: Even though 10 gets autoboxed to Integer, the type check still applies! Integer is not related to TestingClass, so it fails.
Wrapper Classes Map
intInteger
floatFloat
doubleDouble
charCharacter
booleanBoolean
byteByte
shortShort
longLong
Golden Rule: Autoboxing converts primitives to wrappers, but it does NOT bypass array type safety checks!
6
Complete Rules & Quick Test
πŸ† The 6 Golden Rules
1. Declaration: Use Type[] not Type to enable array indexing with []
2. Type Safety: Arrays only accept declared type or its subclasses
3. Object[] Exception: Only Object[] accepts ANY object type
4. Autoboxing: Primitives convert to wrappers, but type checking still applies
5. Null Elements: Array creation doesn't create objectsβ€”all start as null
6. Inheritance: Child objects βœ… fit in parent array, parent objects ❌ don't fit in child array
⚑ Quick Self-Test
class A {}
class B extends A {}
class C {}

A[] arr = new A[3];
arr[0] = new A(); // βœ… Parent
arr[1] = new B(); // βœ… Child allowed
arr[2] = new C(); // ❌ Unrelated class
arr[0] = 10; // ❌ Integer β‰  A

Object[] obj = new Object[3];
obj[0] = new A(); // βœ… Everything extends Object
obj[1] = 10; // βœ… Autoboxed to Integer
obj[2] = "hi"; // βœ… String is Object
🎯 Common Mistakes to Avoid
  • Using Object instead of Object[] - Cannot index plain Object
  • Forgetting to create objects - Array holds references, not objects
  • Putting parent in child array - Causes ArrayStoreException
  • Thinking autoboxing bypasses type checks - It doesn't!
  • Wrong casting from Object[] - Causes ClassCastException
🎫 Ultimate Memory Trick:
"Arrays are VIP clubs with strict guest lists. Only the declared type and its kids get in. But Object[]? That's an open partyβ€”everyone's invited because everyone IS-A Object!"