Parameter Passing Revisited Overview l Parameter passing l






![Parameter Passing : Passing Objects public static void main (String[] args) throws IOException { Parameter Passing : Passing Objects public static void main (String[] args) throws IOException {](https://slidetodoc.com/presentation_image_h2/33210b83e27943a6bec5cb6ff2c1396e/image-7.jpg)


- Slides: 9

Parameter Passing Revisited Overview l Parameter passing l Passing parameters by value l Passing parameters by reference l Some Examples l Preview: Introduction to Recursion 1

Parameter Passing Revisited l In Java variables of primitive types are Passed by value while objects are passed by reference l “Passing by value” means that the argument’s evaluated value is copied, and is passed to the method. » Inside the method, any modification to the variable does not affect the original argument l Passing by reference means that a reference to (i. e. , the address of) the argument is passed to the method. » Using the reference, the called method is actually directly accessing the argument, not a copy of it » Any changes the method makes to the parameter also affects the actual object used as the argument » So after you return from the method, that object will retain any new values set in the method 2

Parameter Passing : Passing Values import java. io. *; class Parameter. Passing { static void modify. Parameter (int num) { num = num * num + 3; System. out. println("Value inside method is: " + num); } public static void main (String[] args) throws IOException { Buffered. Reader stdin= new Buffered. Reader( new Input. Stream. Reader(System. in)); int n, n 1; System. out. println("Enter an integer: "); n = Integer. parse. Int(stdin. read. Line()); System. out. println("Value before invoking modify. Parameter() is: " + n); modify. Parameter(n); System. out. println("Value after invoking modify. Parameter() is: " + n); } } 3

Parameter Passing : Passing Values import java. io. *; class Swap. Primitives { static void swap(int first, int second) { int temp=first; first=second; second=temp; } public static void main (String[] args) throws IOException { Buffered. Reader stdin= new Buffered. Reader( new Input. Stream. Reader(System. in)); int n 1, n 2; System. out. print("Enter first integer: ”); n 1=Integer. parse. Int(stdin. read. Line()); System. out. print("Enter Second integer: ”); n 2=Integer. parse. Int(stdin. read. Line()); System. out. println("Values before swapping are: "+n 1+" and "+n 2); swap(n 1, n 2); System. out. println("Values before swapping are: "+n 1+" and "+n 2); } } 4

Parameter Passing : Passing Objects class Letter { char c; } public class Parameter. Passing 3 { static void f(Letter y) { y. c = 'z'; } public static void main(String[] args) { Letter x = new Letter(); x. c = 'a'; System. out. println("1: x. c: " + x. c); f(x); System. out. println("2: x. c: " + x. c); } } 5

Parameter Passing : Passing Objects class My. Integer { int n; public My. Integer(int number) { n=number; } } import java. io. *; public class Swap. Objects { static void swap(My. Integer first, My. Integer second) { int temp; temp=first. n; first. n=second. n; second. n=temp; } 6
![Parameter Passing Passing Objects public static void main String args throws IOException Parameter Passing : Passing Objects public static void main (String[] args) throws IOException {](https://slidetodoc.com/presentation_image_h2/33210b83e27943a6bec5cb6ff2c1396e/image-7.jpg)
Parameter Passing : Passing Objects public static void main (String[] args) throws IOException { Buffered. Reader stdin= new Buffered. Reader( new Input. Stream. Reader(System. in)); int n 1, n 2; System. out. print("Enter first integer: ”); n 1=Integer. parse. Int(stdin. read. Line. read. Int()); System. out. print("Enter Second integer: ”); n 2=Integer. parse. Int(stdin. read. Line. read. Int()); My. Integer int. Object 1 = new My. Integer(n 1); My. Integer int. Object 2 = new My. Integer(n 2); System. out. println("Values Before swapping are: "+n 1+" and "+n 2); swap(int. Object 1, int. Object 2); System. out. println("Values After swapping are: "+int. Object 1. n+" and "+int. Object 2. n); } } 7

Parameter Passing : Passing Objects class Person { int age; String name; public void print(){ System. out. println(age + " " + } name); public Person(int a, String b) { age = a; name = b; } } What will the following output? 8

Parameter Passing : Passing Objects class Test { static int i = 10; public static void main(String[] args) { String str = "First Message. "; Person p 1 = new Person(21, “Khalid"); Person p 2 = new Person(20, “Amr"); mix. Up(i, str, p 1, p 2); System. out. println("i: " + i + " str: " + str); p 1. print(); p 2. print(); } static void mix. Up(int i, String str, Person one, Person two) { i++; str = "Second Message. "; one = two; one. age = 34; one. name = “Ali"; } } 9