1 / 9

Unit 9 - Parameter Passing in Java

Unit 9 - Parameter Passing in Java. Passing Parameters to Constructors and Methods. Any expression that has an appropriate data type can serve as a parameter:. double u = 3, v = - 4; ... Polynomial p = new Polynomial ( 1.0, - (u + v), u * v );

rnagle
Download Presentation

Unit 9 - Parameter Passing in Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Unit 9- Parameter Passing in Java

  2. Passing Parameters to Constructors and Methods • Any expression that has an appropriate data type can serve as a parameter: double u = 3, v = -4; ... Polynomial p = new Polynomial (1.0, -(u + v), u * v); double y = p.getValue (2 * v - u); public class Polynomial { public Polynomial (double a, double b, double c) { ... } public double getValue (double x) { ... } ...

  3. Passing Parameters (cont’d) • int is promoted to double when necessary: ... Polynomial p = new Polynomial (1, -5, 6); double y = p.getValue (3); The same as: (1.0, -5.0, 6.0) The same as: (3.0)

  4. Passing Parameters (cont’d) • Primitive data types are always passed “by value”: the value is copied into the parameter. public class Polynomial { ... public double getValue (double u) { double v; ... } } copy double x = 3.0; double y = p.getValue ( x ); u acts like a local variable in getValue x: 3.0 u:3.0 copy

  5. Passing Parameters (cont’d) public class Test { public double square (double x) { x *= x; return x; } public static void main(String[ ] args) { Test calc = new Test (); double x = 3.0; double y = calc.square (x); System.out.println (x + " " + y); } } x here is a copy of the parameter passed to square. The copy is changed, but... ... the original x is unchanged. Output: 3 9

  6. A Fraction object: num = 5 denom = 17 Passing Parameters (cont’d) • Objects are always passed as references: the reference is copied, not the object. public class Fraction { ... public Fraction add (Fraction f) { ... } } Fraction f1 = new Fraction (1, 2); Fraction f2 = new Fraction (5, 17); Fraction f3 = f1.add (f2); copy reference refers to refers to the same object

  7. Passing Parameters (cont’d) • A method can change an object passed to it as a parameter (because the method gets a reference to the original object). • A method can change the object for which it was called (this object acts like an implicit parameter):

  8. Passing Parameters (cont’d) • Inside a method, this refers to the object for which the method was called. this can be passed to other constructors and methods as a parameter: public class ChessGame { ... Player player1 = new Player (this); ... A reference to this ChessGame object

  9. Example: Suppose the method fun is defined as: public int fun(int x, int y) { y -= x; return y; } What is printed to the screen after the following code is executed? int a = 3, b = 7; b = fun(a, b); a = fun(b, a); System.out.println(a + " " + b);

More Related