Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Methods Overview l Closer Look at Methods l Parameter passing l Passing parameters by value l Passing parameters by reference.

Similar presentations


Presentation on theme: "1 Methods Overview l Closer Look at Methods l Parameter passing l Passing parameters by value l Passing parameters by reference."— Presentation transcript:

1 1 Methods Overview l Closer Look at Methods l Parameter passing l Passing parameters by value l Passing parameters by reference

2 2 Closer Look at Methods l Methods are the fundamental building blocks of a Java program. l They can be instant methods, usually written to implement a particular behavior of an object – e.g. withdraw method l They can also be class method, usually written to compute some value or perform some action – e.g main method or sqrt method of the Math class. l The general format of a method is as follows: Modifiers ReturnType methodName(Parameters) [Exceptions] { Statement1; Statement2; * StatementN; }

3 3 Closer Look at Methods (cont’d) l A method definition could begin with one or more of the following modifiers: [public | private | protected | ] [ static | ] [ final | ] l The first group are called access modifies as they indicate the type of permission or restriction associated with the method. l The following table indicate the meaning of each. l A method can also be static indicating that it is a class method or non-static, indicating that it is an instance method. l A method may also be final, indicating that it can not be overridden by subclasses. classpackagesub-classworld public  protected  default  private 

4 4 Closer Look at Methods (cont’d) Return Type: l The return type indicates the type of value the method returns – int, double, …,or object reference l If no value or reference is returned by a method, the return type is specified as void. l In Java, a method cannot return more than one value. Parameters: l A method may have zero or more parameters. l Parameters are used to receive input from the method caller. l Each parameter is specified by its type and name. l The parameters in a method definition are called formal parameters. l The values used to call the method are called actual parameters. return statement: l If the return type of a method is not void, then there must be at least one return statement that returns the result of the method.

5 5 Methods Invocation An Example: import java.io.*; class MethodInvocation { static double doubleParameter(double num) { double d; d = 2*num; return d; } static double squareParameter (double num){ double sq; sq = num*num; return sq; }

6 6 Methods Invocation public static void main (String[] args) throws IOException { BufferedReader stdin= new BufferedReader( new InputStreamReader(System.in)); double n, d,sq, answer; System.out.println("Enter a number:"); String input = stdin.readLine(); n=Double.parseDouble(input); d = doubleParameter(n); sq = squareParameter(d); System.out.println("Your input is: "+ n); System.out.println("Double of "+n+"is"+d); System.out.println("Square of "+d+"double is "+ sq); }

7 7 Parameter Passing 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 this copy can be modified at will, and 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 are made to 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

8 8 Parameter Passing : Passing Values import java.io.*; class SwapPrimitives { static void swap(int first, int second) { int temp=first; first=second; second=temp; } public static void main (String[] args) throws IOException { BufferedReader stdin= new BufferedReader( new InputStreamReader(System.in)); int n1, n2; System.out.print("Enter first integer: ”); n1=Integer.parseInt(stdin.readLine()); System.out.print("Enter Second integer: ”); n2=Integer.parseInt(stdin.readLine()); System.out.println("Values before swapping are: "+n1+" and "+n2); swap(n1, n2); System.out.println("Values before swapping are: "+n1+" and "+n2); }

9 9 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; }

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


Download ppt "1 Methods Overview l Closer Look at Methods l Parameter passing l Passing parameters by value l Passing parameters by reference."

Similar presentations


Ads by Google