JAVA tip sharing use of return statement

  • 2020-04-01 03:22:52
  • OfStack

A, return a value, this value can be any type. B. return the program to the operating system (that is, terminate the program)
2. In Java, a function can have a return statement with or without a return value type.
The difference, however, is whether a return statement can return a value (depending on the return value type of the function).

A. If the function has a return value type (that is, the return value type is not void), it must have a return statement that returns the value of the corresponding type.
B, if the function does not return a value (that is, the return value type is void), then the return statement in the function (if there is a return statement!) You can't add any variables after. (the function in this case can also have no return statement, but if so, return can only be used as a return to the operating system.)
Such as:

1. There is a return value


public int getAge()
{
    return age;    //Returns the value of the variable age of type int
}

2. No return value     // functions do not have return statements


public void putAge()
{
    System.out.println(age);
}

3. Return to the operating system     The // function does not return a value, but it does have a return statement


public void put(int a) {
        if (a > 0)
            return;      //The return statement is followed by no return value and is used to exit the execution of the program.            
        else
            System.out.println("fasfsa");
}


Related articles: