// This program is designed to introduce the concepts // reference semantics and value semantics. import java.util.*; public class ArrayPractice { public static void main(String[] args) { // value semantics primitives int char boolean double int x = 5; int y = 7; System.out.println("x = " + x + ", y = " + y); swap(x, y); System.out.println("x = " + x + ", y = " + y); // Arrays, objects // Reference semantics String[] words = {"hip", "hip", "hooray"}; String[] words2 = words; // words2 gets a reference to words words2[1] = "pimp"; // this changes words and words2 // With reference semantics, changes that were made // in the modify method will change the words array // above. System.out.println("words = " + Arrays.toString(words)); modify(words); System.out.println("words = " + Arrays.toString(words2)); } // This method takes a reference // to the array words that was made in main. // Since we have its reference, changes made to // the array in this method persist. public static void modify(String[] list) { list[0] = "pimp"; list[2] = "array!"; } // Since we dealing with primitives in this method. // We follow the rules of value semantics. Even though // we do swap the values of a and b within the method // this does not change x and y in main. public static void swap(int a, int b) { int temp = a; a = b; b = temp; System.out.println("a = " + a + ", b = " + b); } }