0

What does return do in java?

For example: static int sum(int val1, int val2) { return val1 + val2; } What does return do here?, and why is the method called static int instead of just static?

3rd Dec 2017, 7:28 PM
Unidentified
Unidentified - avatar
2 odpowiedzi
+ 2
Int declares that the function return an integer. If you use a method to calculate the sum of two numbers, you expect it to return the result back to the caller. So you use return to send this value. Static means that you can call this method, even no object exists for this class. Take an example: You visit a book store and you ask the seller to bring you the book "the player" from Dostoyefsky. You expect him to return this specific book. So you asked for a book (int) and you got it (return). Now lets say you just go there and ask the seller to bring you a book (any). He actually does. And this is static..
3rd Dec 2017, 10:09 PM
Kostas Batz
Kostas Batz - avatar
+ 2
When you have a method that returns a value (like a function), the return statement decides what value it will return. So if you make an addition method, for example, the return statement would return the sum of the numbers. Hope this helps.
3rd Dec 2017, 7:35 PM
Justin Bornais
Justin Bornais - avatar