As the return keyword itself suggest that it is used to return something.At any time in a method, the return statement is used to cause the whole method to return a certain value and ignore all the statements underneath it.
In Java, return is a keyword that is used to exit from the method only with or without returning a value. Every method is declared with a return type in java and it is mandatory for Java methods.
Syntex :-
return;
(or)
return value;
Q.) W.A.P of to sum of two no.?
public class ReturnTypeTest2 {
public int add(int x, int y) { // with arguments
int z = x+y;
return z;
}
public static void main(String args[]) {
ReturnTypeTest2 test = new ReturnTypeTest2();
int add = test.add(10, 20);
System.out.println("The sum of x and y is: " + add);
}
}