A lecture snippet with a mentor as part of the Codegym University course. Sign up for the full course.

"You again, Amigo. Well, hello."

"It seems you are not very happy to see me, Diego?"

"I've gotten the impression that you think that you've already learned everything there is to know about methods in Java."

"Oh, I don't..."

"I don't think so! You don't even know the half of it."

"Sure, I don't disagr...

"Okay, enough talk. It's time to start. And we'll start with something simple. For example, the fact that Java has a return statement. It allows you to instantly terminate a method in which it is called. Here's the statement:

``return;``

"It's simple: the solitary word `return` followed by a semicolon. As soon as the program executes this statement, the current method exits and the calling method continues.

"If `return` is called in the `main` method, then the `main` method will immediately end, and with it the entire program.

Example:

 ``````class Solution {    public static void fill(int[] data, int from, int to, int value)    {      if (from < 0 || to > data.length)        return;      for (int i = from; i < to; i++)      {       data[i] = value;      }    }    public static void main(String[] args)    {      int[] months = {1, 2, 3, 4, 5, 6, 7, 8 ,9, 10, 11, 12};     fill(months, 2, 10, 8);    } }`````` The `fill` method fills part of the passed array with `value`. The part of the array to be filled is defined by the indices `from` and `to`. If `from` is less than `0` or if `to`  is greater than the length of the array, then the method terminates immediately.

"The above program has a `fill` method that fills the array passed to it with `value`. It does not fill the entire array, only the part specified by the indices `from` and `to`.

"At the beginning of the `fill` method, the passed values are checked to ensure that they are valid. If `from` is less than 0, or if `to` is greater than the length of the array, then the `fill` method terminates immediately (executes a `return` statement)."

"Got it. Is that all this statement does?"

"Actually, the `return` statement is more useful than you think. It is so useful that it is found in almost every method in Java. And now I will lead you to an understanding of why this is so.

7
New Java Syntax, level 7, lesson 3
Locked
Welcome! But not everyone.
This is the signIn() method that greets website users. Currently, it greets all users, but it should only greet registered users. All unregistered users have the name "user". Add username validation at the beginning of the signIn() method. If the name is "user", use the return keyword to abort execu

## Methods with a result, `void`

"You probably remember that there are statements, and there are expressions. What's the difference between them?"

"If I'm not mistaken, an expression, unlike a statement, has a meaning that can be used somewhere."

"Correct. And, in Java, methods can also have a value. And this is very good news: methods are not only able to do something based on the input parameters, but also, for example, to evaluate something and return the result of the calculation.

"By the way, you have already encountered such methods:

 ``double delta = Math.abs(d1 - d2);`` The `abs()` method returns a double ``````Scanner console = new Scanner(System.in); int x = console.nextInt();`````` The `nextInt()` method returns an `int` ``````String str = "Hello"; String s2 = str.toUpperCase();`````` The `toUpperCase()` method returns a `String` ``````int[] data = {1, 4, 5, 6, 7, 8, 11}; int[] array = Arrays.copyOf(data, 4);`````` The `copyOf()` method returns an `int[]`

"Each method can only return one value of one predetermined type. The return type is determined when the method is declared:

``````public static Type name(parameters)
{
method body
}``````

Where `name` is the name of the method, `parameters` is the list of method parameters, and `type` is the type of the result that the method returns.

For methods that return nothing, there is a special placeholder type: `void`.

"In other words, if I write my method and don't want to return anything, then I declare the type of the method to be `void`, and that's that?"

"Yep. And, I must also say that Java has quite a lot of such methods.

## Returning a result

"I figured out how to declare a method that returns the result of a calculation/work. How do I return the result from the method itself?"

"That's a valid question. The `return` statement helps us out here once again. Passing a result from a method looks like this:

``return value;``

"The `return` statement will terminate the method immediately. And `value` is what the method should return to the calling method when it exits. The type of `value` must match the `Type` specified in the method declaration. Here are some examples to reinforce what you have learned:

Example 1. The method calculates the minimum of two numbers:

 ``````int min(int a, int b) {    if (a < b)      return a;    else      return b; }`````` The method returns the minimum of two numbers. If `a < b` return `a` Otherwise return `b`

Example 2. The method duplicates the string passed to it `n` times:

 ``````String multiple(String str, int times) {    String result = "";    for (int i = 0; i < times; i++);     result = result + " "+ str;    return result; }`````` The method takes two parameters — a string and the number of times that the string should be repeated. An empty string is created for the future result. In a loop with `times` iterations, a space and the string `str` is added to the string `result`. The string `result` is returned as the result of the method.

Example 3: The method calculates the maximum of two numbers using the ternary operator:

 ``````int max(int a, int b) {    return (a > b ? a : b); }`````` The method returns the maximum of two numbers. return (if `a > b`, then `a`, otherwise `b`)

"Excellent. Methods are my new superpower!"

"Only if you practice enough in hands-on tasks. Over and out."

7