Creating Methods in Java

In JavaScript, we created functions; in Java, everything is object-oriented, so we'll be creating methods. A method is just a function that's part of a class or object. As an example, I'll create a method now named adder that will add two integers and return their sum.

To start, I'll need two numbers to add, and I'll let the user enter them as command-line arguments. I can read those arguments from the array passed to the main method, which I name args, and then store them in integers value1 and value2, like this:

public class app
{
    public static void main(String[] args)
    {
        int value1 = Integer.parseInt(args[0]);
        int value2 = Integer.parseInt(args[1]);
    .
    .
    .
}

Now I display those values, pass them to the adder method, and display the value that adder returned, like this:

public class app
{
    public static void main(String[] args)
    {
        int value1 = Integer.parseInt(args[0]);
        int value2 = Integer.parseInt(args[1]);

        System.out.println(value1 + " + " + value2 +
        " = " + adder(value1, value2));
    }
    .
    .
    .
}

All that remains is to create the adder method. You can give methods access specifiers such as public or private. If you give it the access specifier public, the method is accessible outside the object or class. If you give it the access specifier private (which is the default if you don't use an access specifier), it's accessible only inside the object or class. I'll use public here.

In addition, you must specify the return type of the value the method returns (you can use the keyword void if the method doesn't return a value). And you must give a comma-separated argument list for the method, giving the type of each argument in parentheses following the method's name (if the method takes no arguments, leave the parentheses empty). All this gives us the following skeleton for the definition of adder:

public class app
{
    public static void main(String[] args)
    {
        int value1 = Integer.parseInt(args[0]);
        int value2 = Integer.parseInt(args[1]);

        System.out.println(value1 + " + " + value2 +
        " = " + adder(value1, value2));
    }
    public static int adder(int int1, int int2)
    {
    .
    .
    .
    }
}

In the body of the method, I can refer to the two values passed using the names I've given them in the argument list, int1 and int2. I add those values and return the result using the return statement:

public class app
{
    public static void main(String[] args)
    {
        int value1 = Integer.parseInt(args[0]);
        int value2 = Integer.parseInt(args[1]);

        System.out.println(value1 + " + " + value2 +
        " = " + adder(value1, value2));
    }

    public static int adder(int int1, int int2)
    {
        return int1 + int2;
    }
}

Now the user can enter values to add on the command line, and the application will handle them without problem:

%java app 180 120
180 + 180 = 300

Using return

You can use the return statement even in methods that you don't return any value from if you want to terminate execution and return from the method—just use the return statement alone, without specifying any values to return.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.225.149.232