return-English literal translation return, return the meaning;
In Java, return represents the meaning of the returned data. In Java, you typically define a method that implements a function.
Package Sreturn;
public class Sreturn {
public int Sreturn (int a, int b) {
int C = a + B;
return C;
}
}
The previous section of the code implements the addition c=a+b; returns an int of type C. In other words, this method implements a simple addition function, which is implemented to return a value of C (note that returning only gives the user a data type that can be used, not the output system). If there is no return to the result of this addition, then our code and method are meaningless.
Here are three examples that return different data types:
1.public String Sreturn2 () {
String name = "Happy";
return name;
}
2.public double Sreturn3 (double c,double d) {
Double F = c/d;
return F;
}
3.public Boolean Sreturn4 () {
Boolean a= false;
return A;
}
The above three examples implement different types of return values String,double and Boolean, respectively.
So when do I use return? With a return every time? What is the time when you don't use return?
There are two ways to use return in Java:
1. Returns the value of the corresponding type;//If the method declares a data type, it must return the same data type.
2. End the execution of the program;//A return statement that represents the end of the statement execution.
The second example is as follows:
if (a>4) {
Return
}
else{
System.out.println ("xxxx");
}
When the program does not need to return a value, we need to use the Void keyword as follows:
public void Speaking () {
System.out.println ("We will speak");
}
This example uses the Void keyword, we do not need to return any value, it is just a method, we use this method will output "We can talk." When the Void keyword appears in the program, it does not need to be returned or return.
The return and void keywords can control the flow of the method.
Thank you for the evaluation points and pros.
Java Beginner return Knowledge point