General question: What does return mean?


#1

I keep seeing this being referred to and I'm not sure what it means. Examples being "Return type" or "Return Value". Thank you!


#2

@emham,
With a
google search
java return statement site:oracle.com
found
https://docs.oracle.com/javase/tutorial/java/javaOO/returnvalue.html


#3

The return keyword stops the execution of a method, and gives a value as a result for the method's caller to use.

For example:

    public class Foo {
        public static void main(String[] args) {
            //The getRandomNumber() method returns the number 4, which is printed in this code.
            System.out.println(getRandomNumber());
        }

        public int getRandomNumber() {
            return 4; //This is an Xkcd reference. :P
        }
    }

4 is the return value of the getRandomNumber(). int is the return type, because 4 is an int. The return type is specified on line 7 (public int getRandomNumber)