Tutorial 4 Values and References Here are some "What Does it Print?" style problems you can go through with your students to discuss these concepts: Add One A public static void addone(int num) { num++; int x = 0; addone(x); System.out.println(x); Add One B public static int addone(int num) { num++; return num; int x = 0; x = addone(x); System.out.println(x); Add One C public static void addone(int[] anarray) { anarray[0]++; int[] a = { 0, 1 ; addone(a); for (int i = 0; i < a.length; i++) { System.out.println(a[i]);
Add One D public static void addone(int[] anarray) { anarray = new int[2]; int[] a = { 0, 1 ; addone(a); for (int i = 0; i < a.length; i++) { System.out.println(a[i]); Add One E public static int[] addone(int[] anarray) { anarray = new int[2]; return anarray; int[] a = { 0, 1 ; a = addone(a); for (int i = 0; i < a.length; i++) { System.out.println(a[i]); Object Call A class Number { int x; public class Reference { public static void main ( String[] args ) { Number a = new Number(); a.x=4; System.out.println(a.x); Number b=a; b.x=5; System.out.println(b.x);
Object Call B class Operation{ int data; public Operation() { data = 50; void change(int data){ data = data+100; public static void main(string[] args){ Operation op = new Operation(); System.out.println("before change "+op.data); op.change(500); System.out.println("after change "+op.data); Object Call C class Operation2{ int data; public Operation2() { data = 50; void change(operation2 op){ op.data = op.data+100; public static void main(string[] args){ Operation2 op = new Operation2(); System.out.println("before change "+op.data); op.change(op); System.out.println("after change "+op.data); Comparison A
int a = 5; int b = 5; Comparison B class Num { int value; public Num(int v) { value = v; Num a = new Num(5); Num b = new Num(5); Comparison C class Num { int value; public Num(int v) { value = v; Num a = new Num(5); Num b = a; Comparison D Integer a = 5 Integer b = 5;
Comparison E Integer a = 128; Integer b = 128; Comparison F Integer a = 128; Integer b = 128; System.out.println(a.equals(b)); Comparison G String a = "5"; String b = "5"; Comparison H String a = new String("5"); String b = new String("5"); Comparison I String a = new String("5"); String b = new String("5"); System.out.println(a.equals(b));
Solutions Add One A: prints 0 due to call by value Add One B: prints 1, function is called by value but the result is returned and assigned Add One C: prints 1 1 due to call by reference (classes and arrays are objects in Java) Add One D: prints 0 1 since new memory is allocated Add One E: prints 0 0 since new memory is allocated, automatically initialised and then returned to replace the original array in the main function Object Call A: prints 4 and 5, assigning reference to original object Object Call B: prints 50, since change function modifies local field and not member (could have called this.data instead) Object Call C: prints 150 due to call by reference Comparison A: prints true for primitives Comparison B: prints false, since comparing references not member values Comparison C: prints true, same reason as B Comparison D: prints true, actually comparing references but Integers are immutuable hence a and b are actually referencing the same memory location Comparison E: prints false, immutuable Integers are only cached from -128 to 127 everything else gets its own memory location (stays immutuable though) Comparison F: prints true, since Integer's equals method checks the actual values Comparison G: prints true, since the string literal "5" is cached by the compiler Comparison H: prints false, since different memory locations are used Comparison I: prints true, same as for comparison F