Closer Look at Methods

Download Report

Transcript Closer Look at Methods

Methods
Overview

Closer Look at Methods

Parameter passing

Passing parameters by value

Passing parameters by reference
1
Closer Look at Methods




Methods are the fundamental building blocks of a
Java program.
They can be instant methods, usually written to
implement a particular behavior of an object – e.g.
withdraw method
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.
The general format of a method is as follows:
Modifiers ReturnType methodName(Parameters)
[Exceptions]
{
Statement1;
Statement2;
*
*
*
StatementN;
}
2
Closer Look at Methods (cont’d)
A method definition could begin with one or more
of the following modifiers:
[public | private | protected | ] [ static | ] [ final | ]
 The first group are called access modifies as they
indicate the type of permission or restriction
associated with the method.
 The following table indicate the meaning of each.

class


package sub-class
public



protected



default
private



world

A method can also be static indicating that it is a
class method or non-static, indicating that it is an
instance method.
A method may also be final, indicating that it can
not be overridden by subclasses.
3
Closer Look at Methods (cont’d)
Return Type:
 The return type indicates the type of value the
method returns – int, double, …,or object reference
 If no value or reference is returned by a method,
the return type is specified as void.
 In Java, a method cannot return more than one
value.
Parameters:
 A method may have zero or more parameters.
 Parameters are used to receive input from the
method caller.
 Each parameter is specified by its type and name.
 The parameters in a method definition are called
formal parameters.
 The values used to call the method are called
actual parameters.
return statement:
 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.
4
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;
}
5
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);
}
}
6
Parameter Passing

In Java variables of primitive types are Passed by
value while objects are passed by reference

“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

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
7
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);
}
}
8
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;
}
}
9
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";
}
}
10
Parameter Passing : Passing Objects
class Letter {
char c;
}
public class ParameterPassing3 {
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);
}
}
11
Parameter Passing : Passing Objects
class MyInteger {
int n;
public MyInteger(int number) {
n=number;
}
}
import java.io.*;
public class SwapObjects {
static void swap(MyInteger first, MyInteger
second) {
int temp;
temp=first.n;
first.n=second.n;
second.n=temp;
}
12