Follow along with the video below to see how to install our site as a web app on your home screen.
Anmerkung: This feature may not be available in some browsers.
Soweit ich weiß bedeutet Return (in dem Fall das die Methode einen Rückgabetyp zugewiesen bekommen hat) dass man die nach dem Schlüsselwort Return angegebene Variable aus dieser Methode zurück gegeben wird und somit auch außerhalb der Methode nutzbar wird.
public void compareStringsTomTailor(String vorName, String nachName)
{
if(vorName.equals("Tom")==false)
{
System.out.println("Der Name ist nicht Tom Tailor");
return;
}
if(nachName.equals("Tailor")==false)
{
System.out.println("Der Name ist nicht Tom Tailor");
return;
}
System.out.println("Der Name ist Tom Tailor");
return; // Dieses Return ist optional
}
Natürlich kann man auch, wenn Methode und andere Teile des Programms in der selben Klasse sind, eine globale variable erstellen welche in der Methode geändert wird und somit ohne Rückgabe außerhalb der Methode verwendbar ist.
public class Foo {
public Foo() {
}
public void doSomething(boolean bedingungErfüllt){
if (bedingungErfüllt) {
System.out.println("Erfolg");
}
else{
return;
}
}
public static void main(String[] args) {
new Foo().doSomething(true);
}
}
true
false
return
public int berechneWerte(int a, int b){
return a + b ;
}
//ausführung
System.out.println(new Foo().berechneWerte(12, 13));
public int additionAB ( int a, int b) {
int x = a+b;
return x;
}