In Java, the return keyword is used to stop execution of a method and return a value for the caller. Usage of this keyword is as following:
return value;
where value is can be of:
For example:
String getName() { return "Peter"; } int getAge() { int myAge = 29; return myAge; } Object getObject() { Object myObject = new Object(); // do something with my object... return myObject; }
The return keyword can also be used to stop execution of a void method and return to the caller, for example:
void doSomething() { // do something if (condition) { return; } }
Related keyword: void. See all keywords in Java.