Return Keyword In Java
The Return keyword is used to return a value in a method
We can only return a single value in a method
The returning value should be of same return type as methods
Return keyword ends the method, in other words -
Once the return keyword is executed it exits the boundary of method , and returns a value.
Now we will see an Example -
Example -
package javaLearnings;
public class Main {
int sample(){
int a = 10;
int b = 20;
int sum = a+b;
return sum;
}
public static void main(String[] args) {
Main ob = new Main();
int b =ob.sample();
System.out.println("the value of a = "+b);
}
}
|
Output -