JavaRush /Java Blog /Random EN /Methods [chapter 2] - Method parameters

Methods [chapter 2] - Method parameters

Published in the Random EN group
Hello, in the previous article we were introduced to the concept of a method. In this article we will continue to talk to you about methods and analyze in detail the parameters of methods . What are method parameters ? Essentially, these are variables that are passed into it (the method) and which it can use in its work. Let's take a closer look with an example. From the previous article , you remember that method parameters are passed in parentheses, but parentheses can also be left empty - this means that the method has no parameters . Let's create a new project - this will be a conditional online store, create an online store cart in it Basket, and add the main method to it, from which our application will start launching. You already know that any application starts executing with the main.
public class Basket {
    public static void main(String[] args) {

    }
}
What can you do with the basket? Firstly, you can add products to it, let's create a special method for this. Let 's call it add()and pass the name of the product and the price of the product into it.
public static void add(String name, int price) {

}
In this method, we have specified two parameters for you. Please note that method parameters are written in parentheses, immediately after the method name. Each parameter is a variable type and a variable name . The variable name is written arbitrarily, you can write whatever you want, but variable names must be specified in such a way that it is clear from them what these variables contain. If there are several parameters, they are written separated by a comma, followed by a space to improve the readability of the code. In order for our basket to work like a real one, we will create a line outside the method in which we will add the added products, we will name it itemsand initially it will be empty.
private static String items = "";
In the method itself, we will add a line break to this line first, so that each new product appears on a new line (so that we can then print this line), a hyphen, the name of the product and the price of the product. As a result, our class looks like this:
public class Basket {

    private static String items = "";

    public static void main(String[] args) {
    }

    public static void add(String name, int price) {
        items = items + "\n" + name + " - " + price;
    }
}
What else can you do with the basket? For example, it can be cleaned. To do this, we will create another method, call it clear()and inside it itemswe will assign an empty string to a variable, thus emptying our trash. To empty the recycle bin, we don’t need parameters, so we write empty parentheses immediately after the method name. This creates methods without parameters.
public static void clear(){
    items = "";
}
Well, to get the full picture, let’s create a method with one parameter that will output the contents of our cart to the console. Let's call it print()and pass the header as a parameter title, which this method should display before the contents of the cart. First, we’ll display the title and then check to see if our cart is empty. Let's call the method on the line isEmptyand if the cart is empty, then we will print the phrase “The cart is empty”, otherwise we will display the contents of our entire cart to the console.
public static void print(String title) {
    System.out.println(title);
    if (items.isEmpty()) {
        System.out.println("Корзина пуста");
    } else {
        System.out.println(items);
    }
}
Please note that from the method we print()call another method (string method isEmpty), which checks whether this string is empty or not. Now let's write code that will call our cart methods from the main(). First, we will add some products using the method add(), then we will call the method with the heading “cart contents”, empty the cart and call the method with the same heading print()again .print()
public class Basket {

    private static String items = "";

    public static void main(String[] args) {
        add("Вобла", 169);
        add("Пиво", 120);
        add("Чипсы", 160);
        print("Содержимое корзины");
        clear();
        print("Содержимое корзины");
    }

    public static void add(String name, int price) {
        items = items + "\n" + name + " - " + price;
    }

    public static void clear(){
        items = "";
    }
    public static void print(String title) {
        System.out.println(title);
        if (items.isEmpty()) {
            System.out.println("Корзина пуста");
        } else {
            System.out.println(items);
        }
    }
}
Launch the program
Содержимое корзины

Вобла - 169
Пиво - 120
Чипсы - 160
Содержимое корзины
Корзина пуста
In this article, we looked at the parameters of the methods. Method parameters are variables that are specified in parentheses immediately after the method name and that can be used in this method (in its body/code). Methods can have only one parameter, and then the type of the variable and its name are written in parentheses. Also, methods can have several parameters and then they are written like one parameter, but separated by commas. Methods can also have no parameters, in which case the parentheses after the method name remain empty, but the parameter names should be meaningful - this will make your code understandable to others and to yourself. In the next article we'll talk about how methods can return values.
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION