Presentation is loading. Please wait.

Presentation is loading. Please wait.

Mark Fontenot CSE 1341 - Honors Principles of Computer Science I Note Set 7.

Similar presentations


Presentation on theme: "Mark Fontenot CSE 1341 - Honors Principles of Computer Science I Note Set 7."— Presentation transcript:

1 Mark Fontenot mfonten@engr.smu.edu CSE 1341 - Honors Principles of Computer Science I Note Set 7

2 Note Set 7 Overview Parameter Passing Semantics in Java Pass By Value Implication for Primitive Variables Implication for Object References and for String Objects?

3 Pass – by – Value Means that the contents of the argument are copied into the parameter Always the way it is done in Java – but other methods exist in other programming languages public class Test1 { public static void foo(int x) { x++; } public static void main (String[]args) { int z = 10; foo(z); } 10 z x

4 Primitive Variables public class Test1 { public static void foo(int x) { x++; } public static void main (String[]args) { int z = 10; foo(z); } } 10 10 z x when method is called, value of z is copied into x. We can conclude that…

5 Passing an Object Reference public class Test2 { public static void bar(Point x) { x.setLocation(10, 20); } public static void main(String [] args) { Point p = new Point(2, 3); System.out.printf("p = %s\n", p.toString()); bar(p); System.out.printf("now p = %s\n", p.toString()); } p = java.awt.Point[x=2,y=3] now p = java.awt.Point[x=10,y=20] p = java.awt.Point[x=2,y=3] now p = java.awt.Point[x=10,y=20] OUTPUT:

6 Passing an Array public class Test3 { public static void fun(int [] x) { x[0] = 99999; } public static void main(String [] args) { int[] values = new int[3]; values[0] = 1; values[1] = 2; values[2] = 3; System.out.printf("value[0] = %d\n", values[0]); fun(values); System.out.printf("value[0] = %d\n", values[0]); } value[0] = 1 value[0] = 99999 value[0] = 1 value[0] = 99999 OUTPUT:

7 What’s going on here? public class Test2 { public static void bar(Point x) { x.setLocation(10, 20); } public static void main(String [] args) { Point p = new Point(2, 3); System.out.printf("p = %s\n", p.toString()); bar(p); System.out.printf("now p = %s\n", p.toString()); } Main: p 2 3 bar: x The object reference stored in p gets copied into x. System Stack

8 What’s going on here? public class Test2 { public static void bar(Point x) { x.setLocation(10, 20); } public static void main(String [] args) { Point p = new Point(2, 3); System.out.printf("p = %s\n", p.toString()); bar(p); System.out.printf("now p = %s\n", p.toString()); } } Main: p 10 20 bar: x The object reference stored in p gets copied into x. System Stack

9 Strings mess it up public class Test4 { public static void main (String [] args) { String myString = "Mark"; System.out.printf("MyString 1.0 = %s\n", myString); function(myString); System.out.printf("MyString 2.0 = %s\n", myString); } public static void function(String s) { s = "Hello”; System.out.printf ("s = %s\n", s); } } MyString 1.0 = Mark s = Hello MyString 2.0 = Mark MyString 1.0 = Mark s = Hello MyString 2.0 = Mark OUTPUT: What’s going on here?


Download ppt "Mark Fontenot CSE 1341 - Honors Principles of Computer Science I Note Set 7."

Similar presentations


Ads by Google