0% found this document useful (0 votes)
19 views1 page

Shallow

This Java code shows how a shallow copy of an array is made when passing it to a constructor, so that changes to the original array are reflected in the copy, demonstrating that only a reference and not the array itself was copied.

Uploaded by

Asim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views1 page

Shallow

This Java code shows how a shallow copy of an array is made when passing it to a constructor, so that changes to the original array are reflected in the copy, demonstrating that only a reference and not the array itself was copied.

Uploaded by

Asim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

//code illustrating shallow copy

public class Ex {

private int[] data;

// makes a shallow copy of values


public Ex(int[] values) {
data = values;
}

public void showData() {


System.out.println( Arrays.toString(data) );
}
}
public class UsesEx{

public static void main(String[] args) {


int[] vals = {3, 7, 9};
Ex e = new Ex(vals);
e.showData(); // prints out [3, 7, 9]
vals[0] = 13;
e.showData(); // prints out [13, 7, 9]

// Very confusing, because we didn't


// intentionally change anything about
// the object e refers to.
}
}

You might also like