The return statement:obtaining information from methods


Learn how to use the return command in Java, what is its function and when to use it.In this section of  Progressive Java course will show you how to use the serve and return statement in methods.




Returning useful information  in Java


Although you can not return anything from methods (such as simply display a message or a menu of options, as we did in the last article), the most common is that the methods return something.

How so 'return'?
Return meaning a result. For example, a sum.
The method can be simply a piece of code that calculates the sum of two numbers and returns the result.

Learn how to use the return command in Java, what is its function and when to use it.Or it can be a method that receives the coefficients of a quadratic equation and returns its roots.
The return can be an integer, a float, a string, may simply be a decision (as a boolean) or something more elaborate you want.

But as I said, and again I repeat, create simple methods and the most specific.
Do not create methods that do a thousand things. Instead, create one thousand methods, where each does one thing well determined. This is important for you to organize, not to lose power and reuse their code in future.






Returning integers, floats, doubles ... in Java


In the example of the last article where we create a method that simply showing a message on the screen, he did not return anything. This could be seen by a detail in the method declaration, the 'void'.

To return an integer, we use 'int'.
For example, let's create a method that returns a value of '1 +1'.
To do this, type 'return' and after that we want to return. See:


    public static int soma(){
        return 1+1;
    }


And now, how we use this int return?
It's like asking 'what can we do with a int number?'

Since the method returns an integer, you can see this method as a int number.
That is, you can print, add with another integer, with a float, or simply assign an integer type.
See:


public class returnTest {
    
    public static int sum(){
        return 1+1;
    }
    
    public static void main(String[] args) {
        System.out.print("Declaring the  'sumAns' variable and receiving the return result: "+ sum());
        int sumAns=soma();
        System.out.println(sumAns);
        
        System.out.println("Printing directly the result of from return: " + soma());
        
        System.out.println("Using in a sum 2 + sum() = " + (2 + sum()));
        
        System.out.println("Using in a product: 3 * sum() = " + (3 * sum()));
        }
}



If we want to return a float: return 1.1 * 3.3
We must declare the method as 'public static float sum()' or we get an error.

Returning a String in Java

Let's redo the program from the last Java article in our course. However, instead of printing message on the screen, it will return the string:



public class returnString {
    
    public static String message(){
        return "Progressive Java course!";
    }
    
    public static void main(String[] args) {
        System.out.println("Displaying once: "+ message());
        
        System.out.println("Displaying the message three times:");
        for(int count=1 ; count<=3 ; count++){
            System.out.println(message()));
        }
    }
}


Again, you could have declared a variable of type String to receive the 'return'.
Note that inside the method, it would be the same if we had done:



    public static String message(){
        String message;
        mensagem="Progressive Java course!";
        return message;
    }


Returning a boolean in Java

Let's redo the booleans methods:


    public static boolean true(){;
        return true;
    }

    public static boolean false(){
        return false;
    }


if(true() ) instead if(true)
if(false() ) instead if(false)



So far our methods are not flexible, because always print and return the same thing.
That will change when you learn how the method is used in real programs, through arguments and parameters, which will be the subject of the next article in our online Progressive Java course.


No comments: