Java return statement with Example | Java return keyword

A programming language uses control statements to control the flow of execution of a program. In Java programming, we can control the flow of execution of a program based on some conditions. Java control statements can be put into the following three categories: selection, iteration, and jump. In this tutorial, we will learn about the java jump statement. The jump statement is used to transfer the control to other parts of the program. Java return statement is the last control statement in java.


Java return statement:

The java return statement is used to explicitly return from a method. It causes the program control to transfer back to the caller of the method. The return statement immediately terminates the method in which it is executed. You declare a method return type in its method declaration. Within the body of the method, you use the return statement to return the value. Any method declared void doesn't return a value. If you try to return a value from a method that is declared void, you will get a compiler error. The data type of the return value must match the method's declared return type; you can't return an integer value from a method declared to return a boolean.





Example of java return statement:


class ReturnStatement{
	public int getSum(int a, int b){
		int c = a+b;
		return c;
	}
	public static void main(String args[]){
		ReturnStatement obj = new ReturnStatement();
		int sum = obj.getSum(10, 20);
		System.out.println("Sum of a and b is: " +sum);
	}
}


Output:
Java return statement




you may also like these related posts:

1. Java continue statement with example.

Java return statement with Example | Java return keyword Java return statement with Example | Java return keyword Reviewed by Prashant Srivastava on December 21, 2019 Rating: 5

No comments:

Powered by Blogger.