Bird
Raised Fist0
Javaprogramming~10 mins

Reference data types in Java - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Reference data types
Declare reference variable
↓
Assign object to variable
↓
Variable points to object in memory
↓
Use variable to access object data/methods
↓
Change variable to point to another object or null
↓
Old object may be garbage collected if no references
Reference variables hold addresses to objects in memory, not the actual data. They let you access and manipulate objects indirectly.
Execution Sample
Java
String name = "Alice";
String another = name;
name = "Bob";
System.out.println(another);
This code shows how reference variables point to objects and how changing one variable does not affect the other.
Execution Table
StepActionVariable 'name'Variable 'another'Output
1Declare and assign "Alice" to name"Alice" objectnull
2Assign name to another"Alice" object"Alice" object
3Change name to "Bob""Bob" object"Alice" object
4Print another"Bob" object"Alice" objectAlice
5End"Bob" object"Alice" object
💡 Program ends after printing 'Alice', showing 'another' still points to original object
Variable Tracker
VariableStartAfter 1After 2After 3Final
namenull"Alice""Alice""Bob""Bob"
anothernullnull"Alice""Alice""Alice"
Key Moments - 2 Insights
Why does printing 'another' show 'Alice' even after 'name' changes to 'Bob'?
Because 'another' points to the original "Alice" object, and changing 'name' to "Bob" makes 'name' point to a new object, not affecting 'another' (see execution_table step 4).
Does assigning one reference variable to another copy the object?
No, it copies the reference (address), so both variables point to the same object initially (see execution_table step 2).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what does variable 'name' point to?
Anull
B"Alice" object
C"Bob" object
DSame as 'another'
💡 Hint
Check the 'Variable name' column at step 3 in execution_table
At which step does 'another' get assigned a value?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Look at the 'Action' column describing assignment to 'another' in execution_table
If we changed 'name' to point to null at step 3, what would 'another' point to?
Anull
B"Alice" object
C"Bob" object
DIt would cause an error
💡 Hint
Variable 'another' keeps its reference unless explicitly changed (see variable_tracker)
Concept Snapshot
Reference data types hold addresses to objects, not the data itself.
Assigning one reference to another copies the address, not the object.
Changing one reference variable to a new object does not affect others.
Objects without references can be removed by garbage collection.
Full Transcript
In Java, reference data types store addresses pointing to objects in memory. When you assign one reference variable to another, both point to the same object. Changing one variable to point to a new object does not change the other. For example, if 'name' points to "Alice" and 'another' is assigned 'name', both point to the same "Alice" object. Changing 'name' to "Bob" makes 'name' point to a new object, but 'another' still points to "Alice". This shows how references work differently from simple data copies.

Practice

(1/5)
1.

Which of the following is a reference data type in Java?

int, double, String, boolean
easy
A. String
B. int
C. double
D. boolean

Solution

  1. Step 1: Identify primitive vs reference types

    int, double, and boolean are primitive types storing actual values.
  2. Step 2: Recognize String as a reference type

    String stores a reference to an object containing text data.
  3. Final Answer:

    String -> Option A
  4. Quick Check:

    Reference type = String [OK]
Hint: Remember: String is an object, others are primitives [OK]
Common Mistakes:
  • Confusing String as primitive
  • Thinking int is a reference type
  • Assuming all data types are reference types
2.

Which of the following is the correct way to create a new String object in Java?

?
easy
A. String s = new string("hello");
B. String s = new String("hello");
C. String s = String("hello");
D. String s = new String["hello"];

Solution

  1. Step 1: Check correct constructor syntax

    Use 'new' keyword with class name and parentheses: new String("hello")
  2. Step 2: Verify case sensitivity and brackets

    Class names are case-sensitive (String, not string), and arrays use [] differently.
  3. Final Answer:

    String s = new String("hello"); -> Option B
  4. Quick Check:

    Correct object creation syntax = String s = new String("hello"); [OK]
Hint: Use 'new ClassName()' with exact case for objects [OK]
Common Mistakes:
  • Using lowercase 'string' instead of 'String'
  • Missing 'new' keyword
  • Using array brackets [] incorrectly
3.

What will be the output of the following code?

String a = "hello";
String b = a;
b = b + " world";
System.out.println(a);
medium
A. world
B. hello world
C. hello
D. Compilation error

Solution

  1. Step 1: Understand reference assignment

    Variable b initially points to the same String object as a ("hello").
  2. Step 2: String concatenation creates new object

    b = b + " world" creates a new String object; a still points to original "hello".
  3. Final Answer:

    hello -> Option C
  4. Quick Check:

    Strings are immutable; original stays same [OK]
Hint: String concatenation creates new object, original unchanged [OK]
Common Mistakes:
  • Assuming b changes a's value
  • Thinking references share mutable data
  • Expecting combined string in a
4.

Find the error in the following code snippet:

String[] arr = new String[3];
arr[0] = "Java";
arr[1] = "Python";
arr[3] = "C++";
medium
A. No error, code runs fine
B. Syntax error in array declaration
C. Missing initialization of array elements
D. Array index out of bounds error

Solution

  1. Step 1: Check array size and indices

    Array size is 3, valid indices are 0, 1, 2.
  2. Step 2: Identify invalid index usage

    arr[3] accesses index 3 which is outside valid range, causing runtime error.
  3. Final Answer:

    Array index out of bounds error -> Option D
  4. Quick Check:

    Array indices start at 0, max index = size-1 [OK]
Hint: Array indices go from 0 to length-1 only [OK]
Common Mistakes:
  • Using index equal to array length
  • Confusing syntax error with runtime error
  • Assuming arrays auto-expand
5.

Given the class below, what will be the output?

class Box {
  int length;
  Box(int length) { this.length = length; }
}

public class Test {
  public static void main(String[] args) {
    Box b1 = new Box(5);
    Box b2 = b1;
    b2.length = 10;
    System.out.println(b1.length);
  }
}
hard
A. 10
B. 5
C. Compilation error
D. Runtime error

Solution

  1. Step 1: Understand reference assignment for objects

    b2 is assigned the reference of b1, so both point to the same Box object.
  2. Step 2: Modify object property via one reference

    Changing b2.length to 10 changes the single Box object's length property.
  3. Step 3: Print property via original reference

    Printing b1.length shows 10 because b1 and b2 refer to the same object.
  4. Final Answer:

    10 -> Option A
  5. Quick Check:

    Object references share same data; change reflects everywhere [OK]
Hint: Object references point to same data; changes affect all refs [OK]
Common Mistakes:
  • Thinking b1 and b2 are separate objects
  • Expecting original value after change
  • Confusing primitives with objects
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard