There are two USES of return in Java

  • 2020-04-01 04:37:56
  • OfStack

A return statement in Java is always closely related to a method, and a return statement is always used in a method to return a value of the type specified by the method (which is always determined) and to terminate the execution of the method (just a return statement).

In most articles on return statements, return statements are used only in methods with return values (non-void return values). There is little or no introduction to the use of return statements in the vodi return value method.

The return statement is used in methods of non-void return value types to return not only primitive types, but also objects (including user-defined classes).

The return statement is always used in a method and has two functions:

One is to return the value of the type specified by the method (which is always determined),

One is the execution of the end method (just a return statement).

2:

Instance 1 -- returns a String


private String gets(){
String s = "qw789" ;
return s ;
}

Example 2 -- end the program


private void gets(String s ){
if ( s == null ){
return ;
}
System.out.println( "haha" ) ;
}

Above through two examples to introduce the use of return in Java, I hope to help you.


Related articles: