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
- Store
{10, 20, 30}in anint[]arrayoriginal - Assign
originalto anotherint[]variablecopy - Change
copy[0]to99 - Print the values of
original[0]andcopy[0] - 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