JAVA method - W3school | Tutorialspoint

JAVA method:

Java method is defined as a collection of statements that are joined together to perform an operation.

When you call the System.out.println() method, for an instance, the system executes various statements in sequence to display a note on the console.

Now here you will come to know and learn about your own techniques with or without return values that invokes without and with parameters, and apply the method abstraction in the designed program.

Creating Method:

Take a look on the below example to learn the syntax of a method:

Syntax

public static int methodName(int a, int b) {
// body
}

Here,

  • public static − modifier

  • methodName − name of the method

  • int − return type

  • a, b − formal parameters

  • int a, int b − list of parameters

Method definition includes a method “header” and a method “body”. The same is represented in the following syntax −

Syntax

modifier returnType nameOfMethod (Parameter List) {
// method body
}

The syntax shown above consists−

  • modifier − It describes the access type of method and it can be used optionally

  • returnType − Method might return a value.

  • nameOfMethod − This is the name of the method. The signature method consists of the name method as well as the parameter list.

  • Parameter List − The list of parameters, it is the order, type, as well as number of parameters in a method. These are optional methods, method may includes zero parameters.

  • method body − The method body describes what the method does with the grouped statements.

Example:

This is the source code of the method defined above called max(). This type of method takes two different parameters num1 as well as num2 and the returns the max between the two.

/** the snippet returns the minimum between two numbers */

public static int minFunction(int n1, int n2) {
int min;
if (n1 > n2)
min
= n2;
else
min
= n1;

return min;
}

Method Calling:

It should be called to use it. There are only two ways in which this method is called and that is method returns a value or no return value.

The method calling is extremely simple. When a program invoke this method, the program control attains the transferred to the method calling.Then This method controls the call in two ways:

The following is the example that shows how to describe a method and how to call iot

The process of method calling is simple. When a program invokes a method, the program control gets transferred to the called method. This called method then returns control to the caller in two conditions, when −

  • the return statement is executed.
  • it reaches the method ending closing brace.

The methods returning void is considered as call to a statement. Lets consider an example −

System.out.println(“This is tutorialspoint.com!”);

The method returning value can be understood by the following example −

int result = sum(6, 9);

Example

public class ExampleMinNumber {

public static void main(String[] args) {
int a = 11;
int b = 6;
int c = minFunction(a, b);
System.out.println(“Minimum Value = “ + c);
}

/** returns the minimum of two numbers */
public static int minFunction(int n1, int n2) {
int min;
if (n1 > n2)
min
= n2;
else
min
= n1;

return min;
}
}

This will gives the following result −

Output

Minimum value = 6

The void Keyword:

This allows us to easily create methods which doesn’t return value. In the below illustration we are choosing a method void methodRankPoints. This is a void method, and this will return no value.

Example

public class ExampleVoid {

public static void main(String[] args) {
methodRankPoints
(255.7);
}

public static void methodRankPoints(double points) {
if (points >= 202.5) {
System.out.println(“Rank:A1”);
}else if (points >= 122.4) {
System.out.println(“Rank:A2”);
}else {
System.out.println(“Rank:A3”);
}
}
}

This will offers the following result −

Output

Rank:A1