1. Passing arguments
And now the fun begins. You probably already know from methods like System.out.println()
that we can pass arguments to methods. Once we are inside the method, we refer to them as parameters. In fact, parameters greatly enhance the benefits we get from creating and using methods.
How do we declare a method with parameters? It's actually quite simple:
public static void name(parameters)
{
method body
}
Where name
is the unique name of the method and method body
represents the commands that make up the method. And parameters
is a placeholder for the method parameters, separated by commas. Let's describe this template in more detail:
public static void name(Type1 name1, Type2 name2, Type3 name3)
{
method body
}
Examples:
Code | Explanation |
---|---|
|
The print method is declared with a parameter:String str |
|
The print method is declared with two parameters:String str int count
|
|
The write method is declared with two parameters:int x int y |
If we don't want the method to have parameters, then we just leave the parentheses empty.
Parameters are special variables within a method. With their help, you can pass various values to the method when it is called.
For example, let's write a method that displays a string of text a given number of times.
You already know how to write code to display a string on the screen several times. But which string should you print? And how many times? That's what we need the parameters for.
The code that does this would look like this:
Code | Explanation |
---|---|
|
We declared the printLines method with the following parameters:String text , int count The method displays String text count timesWe call the printLines method with various parameters
|
Each time a method is called, its parameters are assigned the passed values, and only then do we start to execute the commands inside the method.
2. Arguments
I would like to draw your attention a little more to calling a method with parameters.
The values passed to the method are usually called arguments when they are passed to the method.
Let's look at another example:
Code | Explanation |
---|---|
|
We declared the printLines method with the following parameters:String text , int count The method displays String text count timesWe call the printLines method with the following arguments:text = "Hi"; count = 10; text = "Bye"; count = 20; |
When the printLines
method was called for the first time, its parameters were assigned the following values: String text = "Hi", int count = 10
.
When the printLines
method was called the second time, its parameters were assigned different values: String text = "Bye", int count = 20
.
Parameters are no more and no less than variables that are assigned certain values when a method is called. The values "Hi"
, "Bye"
, 10
, and 20
are themselves called arguments."
3. Conflicting variable names when calling a method
Variables can be used as method arguments. This is simple and understandable, but it can potentially produce some difficulties. Let's go back to that same example, but this time we'll move the arguments into separate variables:
Code | Explanation |
---|---|
|
We declared the printLines method with the following parameters:String text , int count The method displays String text count timesWe call the printLines method with the following arguments:text = str; count = n;
|
So far, so good: we have a str
variable. Its value is assigned to the text
parameter when the method is called. We have an n
variable. Its value is assigned to the count
parameter when the method is called." So far, everything is clear.
Now let's rename our variables in the main
method:
Code | Explanation |
---|---|
|
We declared the printLines method with the following parameters:String text , int count The method displays String text count timesWe call the printLines method with the following arguments:text = text; count = count; |
Pay attention to two things
First: we have variables with the same name in different methods. These are different variables (we deliberately depict them using different colors). Everything works the same as in the previous example, where the variables in the main
method were named str
and n
.
Second: Nothing magical happens when the method is called. The parameters are simply assigned the argument values. Regardless of whether they are numbers, strings, variables, or expressions.
After we rename the variables in the main method, nothing has changed. They were different variables in different methods previously, and so they remain. There is no magic connection between the text
and text
variables.
4. Passing references to methods
I hope you understood everything from the previous lesson, because now we're going to revisit passing arguments to methods, only we'll dive deeper.
As you already know, some Java variables store not the values themselves, but instead a reference, i.e. the address of the block of memory where the values are located. This is how string variables and array variables work.
When you assign another array variable to an array variable, what happens? That's right. The two variables start to refer to the same space in memory:
And what happens if one of these variables is a method parameter?
Code | Explanation |
---|---|
|
The printArraySum method calculates the sum of the numbers in the passed array and displays it on the screen |
Exactly the same thing happens: the data
parameter will contain a reference to the same area of memory as the months
variable. When the method is called, a simple assignment occurs: data = months
.
And since both variables refer to the same area of memory storing an integer, then the printArraySum
method can not only read values from the array, but also change them!
For example, we can write our own method that fills a two-dimensional array with the same value. This is how it might look:
Code | Explanation |
---|---|
|
The fill method iterates over every cell in the passed two-dimensional array and assigns value to them.We create a two-dimensional array. We fill the entire array with the number 8 .
|
5. Methods with the same name
Now let's return to method names once again.
Java standards require all methods in the same class to have unique names. In other words, it's impossible to declare two identically named methods in the same class.
When methods are compared for sameness, not only are the names taken into account, but also the types of the parameters! Note that the names of the parameters are not taken into account. Examples:
Code | Explanation |
---|---|
|
These three methods are different methods. They can be declared in the same class. |
|
Each of these five methods is considered different. They can be declared in the same class. |
|
These two methods are considered the same, meaning they cannot be declared in the same class. |
Why are some methods considered the same, while others are different? And why are parameter names not taken into account when determining the uniqueness of a method?
Why is uniqueness necessary at all? The thing is that when the compiler compiles a program, it must know exactly which method you intend to call in any given place.
For example, if you write System.out.println("Hi")
, the compiler is smart and will easily conclude that you intend to call the println()
method here with a String
parameter.
But if you write System.out.println(1.0)
, the compiler will see a call to the println()
method with a double
parameter.
When a method is called, the compiler ensures that the types of the arguments match the types of the parameters. It does not pay any attention to the name of the arguments. In Java, parameter names do not help the compiler determine which method to call. And that's why they are not taken into account when determining the uniqueness of a method.
The name of a method and the types of its parameters are called the method signature. For example, sum(int, int)
Each class must have methods with unique signatures rather than methods with unique names.
GO TO FULL VERSION