What is return in Java and how to use it?
1. Introduction to return
The return statement in Java is always closely related to methods. The return statement is always used in methods. It has two functions. One is to return the value of the type specified by the method (this value is always determined), and the other is to end the execution of the method. (Just a return statement). The return statement is used in methods with non-void return value types. It can not only return basic types, but also objects (including user-defined classes).
2. Return is used for methods in Java and has two functions:
(1) Return the value of the type specified by the method (this value is always determined), or it can be an object
(2) End of method
Two forms:
(1) There is a return type such as: return i;
(2) No return type. For example: return;
Generally, if there is void before a method, it means there is no return value, and if there is no void, there is a return value.
The return keyword is not specifically used to end a loop. The return statement is used to terminate the execution of a function or exit a class method, and return control to the caller of the method. If this method has a return type, the return statement must return a value of this type; if this method does not return a value, you can use a return statement without an expression.
If a method uses a return statement followed by a value of the method's return type, then after calling this method, the result obtained is the value returned by the method.
Example:
When the user inputs two double type values, the sum of the two values needs to be calculated. The two operands can be changed, but the sum function remains unchanged. At this time, a method needs to be defined, as long as Just call this method when you need to sum, and the method will return the calculated result. The code is as follows:
importjava.util.Scanner;publicclassTest{publicstaticvoidmain(String[]args){Scannerinput=newScanner(System.in);System.out.println(Please enter operand 1:);doublenum1=input.nextDouble();//Get Operand input by the user 1System.out.println(Please enter operand 2:);doublenum2=input.nextDouble();//Get the operand input by the user2doubled=sum(num1,num2);System.out.println( num1+++num2+=+d);}/***Create the sum() method and return a double type value**@parami operand 1*@paramj operand 2*@return the sum of the two operands*/ publicstaticdoublesum(doublei,doublej){doublesum=i+j;returnsum;}}
When the user enters two double values, the program calls the sum() method. This method has two parameters, representing operand 1 and operand 2 input by the user respectively. When calling this method, you only need to pass the two values entered by the user, and then the program will execute the sum() method, sum the two numbers, and use the return statement to return the calculated result.
In the main() method, you also need to use a double type variable to receive the value returned by the sum() method, that is, the variable d is the calculated result. The result after running is as follows:
Please enter operand 1: 500 Please enter operand 2: 203500.0+203.0=703.0
Please enter operand 1: 500 Please enter operand 2: 203500.0+203.0=703.0