+ 1
what is the use of return key in java
5 odpowiedzi
+ 15
In simple words
return keyword simple returns a value from a method.
retutn values can be of many types.
for example
int
float
double
byte
char
String
Object
arrays
ListOfObjects like ArrayList, LinkedList.
+ 4
Return is a reserved keyword in Java i.e, we can’t use it as an identifier. It is used to exit from a method, with or without a value.
Tons of examples here: https://www.geeksforgeeks.org/return-keyword-java/
+ 4
Say you call a method from main add(5,10); the add() method will take these arguments and add them together and "return" a value back to the calling method.
int x = add(5,10); once this method is called adds the numbers together and "returns" a value it will look like this.
int x = 15;
+ 2
The return key takes you to a new line when editing code 😀
0
it send result value of method to method which was calling it
public class Program {
public static void main(String[] args) {
System.out.println("sum(10,5) returns "+sum(10,5) );
}
static int sum(int a, int b) {
return a + b;
}
}