What does the return keyword do in a void method in Java?

JavaMethodsReturnVoid

Java Problem Overview


I'm looking at a path finding tutorial and I noticed a return statement inside a void method (class PathTest, line 126):

if ((x < 0) || (y < 0) || (x >= map.getWidthInTiles()) || (y >= map.getHeightInTiles())) {
	return;
}

I'm a novice at Java. Can anyone tell me why it's there? As far as I knew, return inside a void method isn't allowed.

Java Solutions


Solution 1 - Java

It just exits the method at that point. Once return is executed, the rest of the code won't be executed.

eg.

public void test(int n) {
    if (n == 1) {
        return; 
    }
    else if (n == 2) {
        doStuff();
        return;
    }
    doOtherStuff();
}

Note that the compiler is smart enough to tell you some code cannot be reached:

if (n == 3) {
    return;
    youWillGetAnError(); //compiler error here
}

Solution 2 - Java

You can have return in a void method, you just can't return any value (as in return 5;), that's why they call it a void method. Some people always explicitly end void methods with a return statement, but it's not mandatory. It can be used to leave a function early, though:

void someFunct(int arg)
{
    if (arg == 0)
    {
        //Leave because this is a bad value
        return;
    }
    //Otherwise, do something
}

Solution 3 - Java

The keyword simply pops a frame from the call stack returning the control to the line following the function call.

Solution 4 - Java

The Java language specification says you can have return with no expression if your method returns void.

Solution 5 - Java

It exits the function and returns nothing.

Something like return 1; would be incorrect since it returns integer 1.

Solution 6 - Java

It functions the same as a return for function with a specified parameter, except it returns nothing, as there is nothing to return and control is passed back to the calling method.

Solution 7 - Java

See this example, you want to add to the list conditionally. Without the word "return", all ifs will be executed and add to the ArrayList!

    Arraylist<String> list =  new ArrayList<>();
    
    public void addingToTheList() {
    
    if(isSunday()) {
        list.add("Pray today")
        return;
    }
    
    if(isMonday()) {
        list.add("Work today"
        return;
    }

    if(isTuesday()) {
        list.add("Tr today")
        return;
    }
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRelequestualView Question on Stackoverflow
Solution 1 - JavaCookieOfFortuneView Answer on Stackoverflow
Solution 2 - JavaPestoView Answer on Stackoverflow
Solution 3 - JavaMahdeToView Answer on Stackoverflow
Solution 4 - JavaJohn EllinwoodView Answer on Stackoverflow
Solution 5 - JavaAlbertView Answer on Stackoverflow
Solution 6 - JavaChris BallanceView Answer on Stackoverflow
Solution 7 - Javaiali87View Answer on Stackoverflow