0% found this document useful (0 votes)
20 views10 pages

Call by Reference

A method in Java is a block of code designed to perform a specific task, promoting code reusability and readability. Methods can be user-defined and can return values or be invoked without returning anything, with parameters being classified as formal or actual arguments. Method overloading allows multiple methods with the same name but different parameters, enhancing program clarity and efficiency.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views10 pages

Call by Reference

A method in Java is a block of code designed to perform a specific task, promoting code reusability and readability. Methods can be user-defined and can return values or be invoked without returning anything, with parameters being classified as formal or actual arguments. Method overloading allows multiple methods with the same name but different parameters, enhancing program clarity and efficiency.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Method in Java

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.

What is a method in Java?


A method is a block of code or collection of statements or a set of code grouped
together to perform a certain task or operation. It is used to achieve the reusability of
code. We write a method once and use it many times. We do not require to write code
again and again. It also provides the easy modification and readability of code, just
by adding or removing a chunk of code. The method is executed only when we call or
invoke it.

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.

How to Create a User-defined Method

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.

How to Call or Invoke a User-defined Method

Once we have defined a method, it should be called. The calling of a method in a


program is simple. When we call or invoke a user-defined method, the program control
transfer to the called method.

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.

public class Addition


{
public static void main(String[] args)
{
int a = 19;
int b = 5;
//method calling
int c = add(a, b); //a and b are actual parameters
System.out.println("The sum of a and b is= " + c);
}
//user defined method
public static int add(int n1, int n2) //n1 and n2 are formal parameters
{
int s;
s=n1+n2;
return s; //returning the sum
}
}
formal Argument :

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”.

Actual parameters are used in the function calling statement.

Whereas
Formal parameters are used in the function definition statement.

Formal parameters are the copies of actual parameters.

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

• Method overloading is also known as Compile-time Polymorphism,


Static Polymorphism, or Early binding 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.

Different Ways of Method Overloading in Java


• Changing the Number of Parameters.
• Changing Data Types of the Arguments.
• Changing the Order of the Parameters of Methods

1) Method Overloading: changing no. of arguments


In this example, we have created two methods, first add() method performs addition of two
numbers and second add method performs addition of three numbers.
In this example, we are creating static methods so that we don't need to create instance for
calling methods.
class Adder
{
static int add(int a,int b)
{
return a+b;
}
static int add(int a,int b,int c)
{
return a+b+c;
}

public void main()


{
System.out.println(Adder.add(11,11));
System.out.println(Adder.add(11,11,11));
}
}

Method Overloading: changing data type of arguments


In this example, we have created two methods that differs in data type. The first add
method receives two integer arguments and second add method receives two double
arguments.

class Adder1
{
static int add(int a,int b)
{
return a+b;
}
static double add(double a, double b)
{
return a+b;
}

public void main()


{
System.out.println(Adder1.add(11.5,11.5));
System.out.println(Adder1.add(11,11));
}
}

// Java Program to Illustrate Method Overloading


// By changing the Order of the Parameters

// Importing required classes


import java.io.*;

// 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);
}
}

Why Method Overloading is not possible by changing the return


type of method only?
In java, method overloading is not possible by changing the return type of the method
only because of ambiguity. Let's see how ambiguity may occur:

class Adder3
{
static int add(int a,int b)
{

return a+b;
}
static double add(int a,int b)
{
return a+b;
}

public void main()


{
System.out.println(Adder.add(11,11));//ambiguity
}
}

Advantages of Method Overloading


• Method overloading improves the Readability and reusability of the
program.
• Method overloading reduces the complexity of the program.
• Using method overloading, programmers can perform a task
efficiently and effectively.
• Using method overloading, it is possible to access methods
performing related functions with slightly different arguments and
types.
• Objects of a class can also be initialized in different ways using the
constructors.

Some real time examples of method overloading


• The payment option on any ecommerce website has several options like
netbanking, COD, credit card, etc. That means, a payment method is overloaded
several times to perform single payment function in various ways.
• The payment option on any ecommerce website has several options like
netbanking, COD, credit card, etc. That means, a payment method is overloaded
several times to perform single payment function in various ways.

You might also like