JAVA METHODS

Java return and return values

A method can calculate and return a result as well as perform an action. The return type replaces void, and the result is passed back using return.

void or a return value?

Comparing Java method results
Method typeBehaviorExample
voidReturns no valuestatic void pasisveikinti()
intReturns an integerstatic int sudetiKainas()
booleanReturns true or falsestatic boolean arSilta()
StringReturns textstatic String apibudintiOra()

Returning a value

This method accepts an item's original price, subtracts a five-euro discount and returns the new price:

public class Main {
    static int pritaikytiNuolaida(int pradineKaina) {
        return pradineKaina - 5;
    }

    public static void main(String[] args) {
        System.out.println(pritaikytiNuolaida(25));
    }
}
Console output

20

  1. When the method is called, the argument 25 is passed to the parameter pradineKaina.
  2. Inside the method, the expression is evaluated: 25 - 5.
  3. return returns the resulting number 20 to the call site.
  4. The returned value is passed to println(), so the console displays 20.
  5. Since the method's return type is int, the returned result must also be an integer.

A result from multiple parameters

return can return the result of an expression calculated using multiple parameters:

public class Main {
    static int sudetiKainas(int duonosKaina, int pienoKaina) {
        return duonosKaina + pienoKaina;
    }

    public static void main(String[] args) {
        System.out.println(sudetiKainas(3, 2));
    }
}
Console output

5

  1. The method receives the bread price 3 and the milk price 2.
  2. These values go into the parameters duonosKaina and pienoKaina.
  3. return calculates 3 + 2 and returns 5.
  4. println() displays the returned result in the console.

Storing a method's result

You can use a returned value immediately or save it in a variable of a compatible type:

public class Main {
    static int sudetiKainas(int duonosKaina, int pienoKaina) {
        return duonosKaina + pienoKaina;
    }

    public static void main(String[] args) {
        int bendraKaina = sudetiKainas(3, 2);
        System.out.println("Bendra kaina: " + bendraKaina + " eurai");
    }
}
Console output

Bendra kaina: 5 eurai

  1. The method sudetiKainas(3, 2) calculates and returns the number 5.
  2. This time, the result is not printed immediately.
  3. It is saved in the int variable bendraKaina.
  4. On the next line, we combine this variable with explanatory text and display it in the console.
A result variable often makes code clearer.

You can give it a meaningful name, reuse it in several places or check it before continuing the program.

Other return types

A method can return more than numbers. For example, a boolean result works well for evaluating a condition:

public class Main {
    static boolean arSilta(int temperatura) {
        return temperatura >= 20;
    }

    public static void main(String[] args) {
        boolean silta = arSilta(22);
        System.out.println(silta);
    }
}
Console output

true

  1. The method arSilta() receives the temperature 22.
  2. Inside the method, the condition is checked: 22 >= 20.
  3. This condition is true, so return returns the Boolean value true.
  4. The returned value is stored in boolean variable silta.
  5. println() displays in the console true.

return in conditions

Different branches of a method can return different results:

public class Main {
    static String apibudintiOra(int temperatura) {
        if (temperatura < 0) {
            return "Šalta";
        }

        if (temperatura < 20) {
            return "Vėsu";
        }

        return "Šilta";
    }

    public static void main(String[] args) {
        System.out.println(apibudintiOra(15));
    }
}
Console output

Vėsu

  1. The method receives the temperature 15.
  2. The first condition checks 15 < 0. It is false, so Šalta is not returned.
  3. The second condition checks 15 < 20. It is true.
  4. return "Vėsu" returns the text and immediately ends the method.
  5. The final line return "Šilta" is not reached during this call.
When return is reached, the method stops.

If we passed -5, it would return Šalta. If we passed 25, both conditions would be false and the method would return Šilta.

Important return rules

  • The returned value must match the declared return type.
  • A value-returning method must provide a result on every possible execution path.
  • return immediately ends the method's execution.
  • After an unconditional return , the same block cannot contain reachable code.
  • void method can use an empty return; to exit early, but cannot return a value.