// Zorah Fung, CSE 142 // An example of value versus reference semantics. import java.util.*; public class ValueReferenceSemantics { public static void main(String[] args) { int a = 7; int b = 35; swap(a, b); System.out.println("a = " + a + ", b = " + b); // Values will not be swapped int[] array = {11, 42, -5, 27, 0, 89}; reverse(array); System.out.println(Arrays.toString(array)); // Modifications to the array will appear in main } // Swaps values a and b. Because of value semantics, these changes will // not be seen outside of this method. public static void swap(int a, int b) { int temp = a; a = b; b = temp; } // Reverses the given array // Note: Because of reference semantics, a reference to an array is passed and // modifications to the array can be seen outside of this method and thus an array // does not need to be returned. public static void reverse(int[] a) { for (int i = 0; i < a.length / 2; i++) { int temp = a[i]; a[i] = a[a.length - 1 - i]; a[a.length - 1 - i] = temp; } } }