Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

Java Methods

Method

• A method is a block of code which only runs when it is called.

• we can pass data, known as parameters, into a method.


Declaring a Java Method

return-type methodName(parameter-list)

//body of method

}
• returnType - It specifies what type of value a method returns For example if a
method has an int return type then it returns an integer value.

• methodName - is a valid meaningful name that represent name of a method.

• parameter-list - represents list of parameters accepted by this method.


two types of methods:

• User-defined Methods:

We can create our own method based on our requirements.

• Standard Library Methods:

These are built-in methods in Java that are available to use.


User-defined Methods:

7
Parameters and Arguments

• Parameter is variable defined by a method that receives value when the method is
called.

• Argument is a value that is passed to a method when it is called.

8
9
Example

public class Main {


static void myMethod(String fname) {
System.out.println(fname );
}

public static void main(String[] args) {


myMethod(“Sachin");
myMethod(“Manoj");
fname is a parameter
myMethod(“Advith");
Sachin, Manoj and Advith are
}
arguments.
}
10
class mainClass {
public static void main(String[] args)
{
int a = 19;
int b = 5;
//method calling
Example 1

int c = add(a, b);


System.out.println("The sum of a and b is= " + c);
}
//user defined method
public static int add(int n1, int n2)
{
int s;
s=n1+n2;
return s; //returning the sum
}
}
class Main {
public int addNumbers(int a, int b) {
int sum = a + b;
// return value
Example 2

return sum;
}

public static void main(String[] args) {

int num1 = 25;


int num2 = 15;

// create an object of Main


Main obj = new Main();
// calling method
int result = obj.addNumbers(num1, num2);
System.out.println("Sum is: " + result);
} Sum is: 40
}
class Main {

// create a method
public static int square(int num) {

// return statement
Example 3

return num * num;


}

public static void main(String[] args) {


int result;

// call the method


// store returned value to result
result = square(10);

System.out.println("Squared value of 10 is: " + result);


}
Squared value of 10 is: 100
}
Java Standard Library Method (built in methods)
Example 1

public class mainClass{

public static void main(String[] args)

System.out.print("Square root of 4 is: " + Math.sqrt(4));

}
Output:
Square root of 4 is: 2.0
Advantages of using methods

16
What are the advantages of using methods?

• Main advantage is code reusability.

• We can write a method once, and use it multiple times.

• We do not have to rewrite the entire code each time.

• "write once, reuse multiple times".


Example :Java Method for Code Reusability
public class Main {
// method defined
private static int getSquare(int x){
return x * x;
}
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
int result = getSquare(i);
System.out.println("Square of " + i + " is: " + result);
}
}}
Output:

• Square of 1 is: 1

• Square of 2 is: 4

• Square of 3 is: 9

• Square of 4 is: 16

• Square of 5 is: 25

You might also like