Co robi słowo kluczowe return w metodzie void w Javie?

Patrzę na poradnik odnajdywania ścieżki i zauważyłem return Instrukcję wewnątrz metody void (klasa PathTest, linia 126):

if ((x < 0) || (y < 0) || (x >= map.getWidthInTiles()) || (y >= map.getHeightInTiles())) {
    return;
}
Jestem nowicjuszem w Javie. Czy ktoś może mi powiedzieć, dlaczego tam jest? O ile wiem, return wewnątrz metody void nie jest dozwolone.
Author: Boann, 2009-04-13

7 answers

W tym momencie wychodzi z metody. Po wykonaniu return, reszta kodu nie zostanie wykonana.

Np.

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

Zauważ, że kompilator jest wystarczająco inteligentny, aby powiedzieć, że niektóre kody nie mogą być osiągnięte:

if (n == 3) {
    return;
    youWillGetAnError(); //compiler error here
}
 102
Author: CookieOfFortune,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-06-29 20:21:48

Możesz mieć return w metodzie void, po prostu nie możesz zwrócić żadnej wartości (jak w return 5;), dlatego nazywają ją metodą void . Niektórzy ludzie zawsze wyraźnie kończą metody void instrukcją return, ale nie jest to obowiązkowe. To Może być użyte do wcześniejszego opuszczenia funkcji, choć:

void someFunct(int arg)
{
    if (arg == 0)
    {
        //Leave because this is a bad value
        return;
    }
    //Otherwise, do something
}
 23
Author: Pesto,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-11-22 11:07:56

Słowo kluczowe po prostu wyświetla ramkę ze stosu wywołań, zwracając kontrolkę do linii następującej po wywołaniu funkcji.

 15
Author: MahdeTo,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-04-13 17:36:00

Specyfikacja języka Java mówi, że możesz mieć return bez wyrażenia, jeśli twoja metoda zwróci wartość void.

 12
Author: John Ellinwood,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-04-13 17:32:53

Działa tak samo jak return for funkcji z podanym parametrem, z tym, że nie zwraca nic, ponieważ nie ma nic do zwrócenia i kontrola jest przekazywana z powrotem do metody wywołującej.

 2
Author: Chris Ballance,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-04-13 17:33:16

Kończy funkcję i nic nie zwraca.

Coś w rodzaju return 1; byłoby niepoprawne, ponieważ zwraca liczbę całkowitą 1.

 2
Author: Albert,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-04-13 17:33:48

Zobacz ten przykład, chcesz dodać do listy warunkowo. Bez słowa "return" wszystkie ifs zostaną wykonane i dodane do 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;
    }
}
 1
Author: iali87,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-10-20 12:25:58