001-002-019

Reference Type Assignment and Object Sharing

Hard

Problem Description

Reference Type Assignment and Object Sharing

In this problem, you will create a program that assigns an int[] array to another variable, modifies copy[0], and verifies that the change is also reflected in original[0], then displays the result to standard output.

Learning Objective: Understand that assigning reference type variables means sharing the same object

Overview

Create a program that assigns an array variable to another variable and demonstrates that modifying one affects the other.

Specifications

  1. Store {10, 20, 30} in an int[] array original
  2. Assign original to another int[] variable copy
  3. Change copy[0] to 99
  4. Print the values of original[0] and copy[0]
  5. Check if they reference the same object using == and print the result

Output Format

original[0]: 99
copy[0]: 99
Same object: true

Ready to Try Running Code?

Log in to access the code editor and execute your solutions for this problem.

Don't have an account?

Sign Up