Call by Reference
Call by Reference
In general, a method is a way to perform some task. Similarly, the method in Java is
a collection of instructions that performs a specific task. It provides the reusability of
code.
Method Declaration
The method declaration provides information about method attributes, such as
visibility, return-type, name, and arguments. It has six components that are known
as method header, as we have shown in the following figure.
User-defined Method
The method written by the user or programmer is known as a user-defined method.
These methods are modified according to the requirement.
Let's create a user defined method that checks the number is even or odd. First, we
will define the method.
//user defined method
public void findEvenOdd(int num)
{
//method body
if(num%2==0)
System.out.println(num+" is even");
else
System.out.println(num+" is odd");
}
We have defined the above method named findevenodd(). It has a parameter num of
type int. The method does not return any value that's why we have used void. The
method body contains the steps to check the number is even or odd. If the number is
even, it prints the number is even, else prints the number is odd.
import java.util.Scanner;
public class EvenOdd
{
public void main ()
{
//creating Scanner class object
Scanner scan=new Scanner(System.in);
System.out.print("Enter the number: ");
//reading value from the user
int num=scan.nextInt();
//method calling
findEvenOdd(num);
}
In the above code snippet, as soon as the compiler reaches at line findEvenOdd(num), the
control transfer to the method and gives the output accordingly.
Let's see another program that return a value to the calling method.
In the following program, we have defined a method named add() that sum up the
two numbers. It has two parameters n1 and n2 of integer type. The values of n1 and
n2 correspond to the value of a and b, respectively. Therefore, the method adds the
value of a and b and store it in the variable s and returns the sum.
The formal arguments are the arguments in the function declaration. The scope of
formal arguments is local to the function definition in which they are used. They belong
to the called function.
Actual arguments :
The arguments that are passed in a function call are called actual arguments. These
arguments are defined in the calling function.
To sum up in one line — Calling programs pass information to called functions in "actual
arguments” & the called functions access the information using their corresponding
"formal arguments”.
Whereas
Formal parameters are used in the function definition statement.
Call by value:
If we call a method passing a value, it is known as call by value. The changes being done in the called
method, is not affected in the calling method.
class Swap1
{
void exchange(int x,int y)
{
int z=x;
x=y;
y=x;
}
public void main()
{
int x=10,y=20;
System.out.println("before exchange");
System.out.println("x="+x+"y="+y);
exchange(x,y);
System.out.println("after exchange");
System.out.println("x="+x+"y="+y);
}
}
Call by Reference
Call by reference means we call a method by passing reference (location of the variable) in a
parameter. In Java, primitive data types (Int, float, double, Boolean, char, etc.) are always
passed as values and Non-Primitive data types (class, object, array, string, and interface) are
always passed as a reference.
class Swap2
{
int x,y;
void exchange(Swap2 s)
{
int z=x;
x=y;
y=z;
}
public void main()
{
Swap2 obj=new Swap2();
x=10;
y=20;
System.out.println("before exchange");
System.out.println("x= "+x+" y="+y);
exchange(obj);
System.out.println("after exchange");
System.out.println("x= "+x+" y="+y);
}
}
Method overloading in JAVA
• If a class has multiple methods having same name but different in parameters, it is
known as Method Overloading.
• If we have to perform only one operation, having same name of the methods increases
the readability of the program.
• Suppose you have to perform addition of the given numbers but there can be any
number of arguments, if you write the method such as a(int l, int mm) for two
parameters, and b(int,int,int) for three parameters then it may be difficult for you as
well as other programmers to understand the behavior of the method because its name
differs.
class Adder1
{
static int add(int a,int b)
{
return a+b;
}
static double add(double a, double b)
{
return a+b;
}
// Class 1
// Helper class
class Student
{
// Method 1
public void StudentId(String name, int roll_no)
{
System.out.println("Name :" + name + " " + "Roll-No :" + roll_no);
}
// Method 2
public void StudentId(int roll_no, String name)
{
// Again printing name and id of person
System.out.println("Roll-No :" + roll_no + " " + "Name :" + name);
}
}
class Adder3
{
static int add(int a,int b)
{
return a+b;
}
static double add(int a,int b)
{
return a+b;
}