lecture07

lecture07 - Session 7 Methods Strings Constructors this...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
Session 7 Methods Strings Constructors this Inheritance Java Methods Methods are the interface or communications between classes They provide a way to invoke the same operation from many places in your program, avoiding code repetition They hide implementation details from the caller or user of the method Variables defined within a method are not visible to callers of the method; they have local scope within the method. The method cannot see variables in its caller either. There is logical separation between the two, which avoids variable name conflicts. 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Invoking a Method public class Power1 { public static void main(String[] args) { double a= 10.0, b; int x= 3; System.out.println("Main: a= " + a + ", x= " + x); b= power(a, x); System.out.println("Main: Result b= " + b); } public static double power(double d, int i){ System.out.println("Power: d= " + d + ", i= " + i); double x= d; // Different x than main x for (int j= 1; j < i; j++) x *= d; // x = x * d; return x; } } ) { / // of d and i implicitly return x;} double power(double d, int i / Method makes own copy double x= d; Passing Arguments void main(String[ ] args){… Communi- double a; cation only int x; via arg list, b= power(a, x); return value Arguments matched by Return value Argument 1 Argument 2 position Assume method is written first: can’t know main() vars 2
Background image of page 2
Call By Value Java supports only call-by-value when passing arguments to a method: The method makes a local copy of each argument It then operates on that local copy A method cannot change the value of an argument (variable) in the method that calls it Call By Value A method can only send information back to its caller Via the return value, and By changing the data in objects passed to it (more on this later) The return value can be void, meaning nothing is returned. Method main() has a void return. The other mechanism, supported by C++ and other languages, is call-by-reference, which allows the method to change the arguments. 3
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Call By Value Example public class CallByValue { public static void main(String[] args) { int i= 3; double d= 77.0; System.out.println("Main 1: i= " + i + ", d= " + d); triple(i, d); // No return value System.out.println("Main 2: i= " + i + ", d= " + d); // Secondary part of example: argument conversion triple(i, i); // Ok-Java converts int to double System.out.println("Main 3: i= " + i); } public static void triple(int ii, double dd) { System.out.println(“Triple 1: ii= " +ii+ ", dd= " +dd); ii *= 3; // ii= ii*3; dd *= 3.0; System.out.println(“Triple 2: ii= " +ii+ ", dd= " +dd); } } Call By Value With Object public class CallObjExample { public static void main(String[] args) { Demo d= new Demo(3); int i= 3; System.out.println("Main1: i= " + i + ", d.a= " + d.a); triple(i, d); // No return value System.out.println("Main2: i= " + i + ", d.a= " + d.a); } public static void triple(int ii, Demo dd){ System.out.println("T1: ii= "+ ii + ", dd.a= " + dd.a); ii *= 3; // ii= ii*3; dd.a *= 3; System.out.println("T2: ii= "+ ii + ", dd.a= " + dd.a); } } 4
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 17

lecture07 - Session 7 Methods Strings Constructors this...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online