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?
| Method type | Behavior | Example |
|---|---|---|
void | Returns no value | static void pasisveikinti() |
int | Returns an integer | static int sudetiKainas() |
boolean | Returns true or false | static boolean arSilta() |
String | Returns text | static 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));
}
}20
- When the method is called, the argument
25is passed to the parameterpradineKaina. - Inside the method, the expression is evaluated:
25 - 5. returnreturns the resulting number20to the call site.- The returned value is passed to
println(), so the console displays 20. - 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));
}
}5
- The method receives the bread price
3and the milk price2. - These values go into the parameters
duonosKainaandpienoKaina. returncalculates3 + 2and returns5.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");
}
}Bendra kaina: 5 eurai
- The method
sudetiKainas(3, 2)calculates and returns the number5. - This time, the result is not printed immediately.
- It is saved in the
intvariablebendraKaina. - On the next line, we combine this variable with explanatory text and display it in the console.
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);
}
}true
- The method
arSilta()receives the temperature22. - Inside the method, the condition is checked:
22 >= 20. - This condition is true, so
returnreturns the Boolean valuetrue. - The returned value is stored in
booleanvariablesilta. 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));
}
}Vėsu
- The method receives the temperature
15. - The first condition checks
15 < 0. It is false, so Šalta is not returned. - The second condition checks
15 < 20. It is true. return "Vėsu"returns the text and immediately ends the method.- The final line
return "Šilta"is not reached during this call.
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.
returnimmediately ends the method's execution.- After an unconditional
return, the same block cannot contain reachable code. voidmethod can use an emptyreturn;to exit early, but cannot return a value.