"Hi Amigo! I hope you enjoyed solving tasks using your own methods, and that you realize how convenient it is to create methods. Now let's talk about the most interesting topic of all."

"You've piqued my interest, Diego... A new topic?"

"Every topic is new for you, my young robot! And this one is no exception. Though once again it is about methods. You've probably already noticed 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."

"Parameters are what we write inside the parentheses?"

"Yes, exactly. And, in fact, parameters greatly enhance the benefits we get from creating and using methods."

"I get what you're saying about using them, and it turns out that I've already done it. 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."

"Hmm... I think I understand... Or maybe not..."

"Let me give you some more details about this template so you are certain that you understand that you understand:

public static void name(Type1 name1, Type2 name2, Type3 name3)
{
  method body
}

Here are some examples:

Code Explanation
public static void print(String str)
{
}
The print method is declared with a parameter:
String str
public static void print(String str, int count)
{
}
The print method is declared with two parameters:
String str
int count
public static void write(int x, int y)
{
}
The write method is declared with two parameters:
int x
int y

"Ah... Now it's clear. And if we don't want the method to have parameters, then we just leave the parentheses empty."

"Exactly. Basically, parameters are special variables within a method. With their help, you can pass various values to the method when it is called. And if you don't need to pass values, then you leave the parentheses empty.

"For example, let's write a method that displays a given line of text a given number of times. Any thoughts on how to do this?"

"Well... It seems I'm well aware of how to write code to display a string on the screen several times..."

"How do you specify the string to be displayed? And how do you specify the number of displayed lines? Can you guess?"

"Things are starting to become clear... Probably with the help of method parameters?"

"Exactly. A string parameter for the line of text, and a numeric parameter for the number of displayed lines. The code that does this would look like this:

Code Explanation
class Solution
{
   public static void printLines(String text, int count)
   {
     for (int i = 0; i < count; i++)
       System.out.println(text);
   }

   public static void main(String[] args)
   {
     printLines("Hi", 10);
     printLines("Bye", 20);
   }
}

We declared the printLines method with the following parameters:
String text, int count

The method displays String text count times



We 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.

Arguments

"I want you to pay special attention to calling methods that have parameters. The values passed to the method are usually called arguments when they are passed to the method.

Let's take another look at our example:

Code Explanation
class Solution
{
   public static void printLines(String text, int count)
   {
     for (int i = 0; i < count; i++)
       System.out.println(text);
   }

   public static void main(String[] args)
   {
     printLines("Hi", 10);
     printLines("Bye", 20);
   }
}

We declared the printLines method with the following parameters:
String text, int count

The method displays String text count times


We 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."

"I'll try to remember the difference and not confuse these concepts."

Conflicting variable names when calling a method

"When you call a method, you can use variables as arguments.

"Well, that makes sense!"

"It makes sense, but it can potentially produce some difficulties. Let's go back to our example once again, but this time we'll move the arguments into separate variables:

Code Explanation
class Solution
{
   public static void printLines(String text, int count)
   {
     for (int i = 0; i < count; i++)
       System.out.print(text);
   }

   public static void main(String[] args)
   {
     String str = "Hi";
     String n = 10;
     printLines(str, n);
   }
}

We declared the printLines method with the following parameters:
String text, int count

The method displays String text count times



We call the printLines method with the following arguments:
text = str;
count = n;

"Hmm... I don't see any difficulties. 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."

"Good, good. Now let's rename our variables in the main method:

Code Explanation
class Solution
{
   public static void printLines(String text, int count)
   {
     for (int i = 0; i < count; i++)
       System.out.print(text);
   }

   public static void main(String[] args)
   {
     String text = "Hi";
     String count = 10;
     printLines(text, count);
   }
}

We declared the printLines method with the following parameters:
String text, int count

The method displays String text count times



We 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 two text variables."

"Now I know."

7
Task
New Java Syntax,  level 7lesson 2
Locked
Favorite dish
Rename the parameters of the printPersonInfo() method: - firstName to name; - lastName to surname; - favoriteDish to meal; without changing the program's functionality. Leave the variable names in the main() method unchanged.
7
Task
New Java Syntax,  level 7lesson 2
Locked
The Liverpool 4
The createCrew() method displays the positions and names of the crew members on a spacecraft. To avoid any confusion inside the method, change the names of the createCrew() method's parameters to match the names of the variables passed to the method: - name1 to navigator - name2 to pilot - name3 in

Passing references to methods

"I hope you've already assimilated everything I've told you about passing arguments to methods. I say that, because we're now going to dive a little deeper into this topic. Listen carefully."

"You already know that some variables in Java 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 a developer assigns another array variable to an array variable, what happens?"

"Do they then point at the same address?"

"Correct. 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
class Solution
{
   public static void sum(int[] data)
   {
     int total = 0;
     for (int i = 0; i < data.length; i++)
       total = total + data[i];
     System.out.println(total);
   }

   public static void main(String[] args)
   {
     int[] months = {31, 28, 31, 30, 31, 30, 31, 31, 30};
     sum(months);
   }
}


The sum 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 sum method can not only read values from the array, but also change them!"

"I guess I understand, but I need more examples!"

"Well, 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
class Solution
{
   public static void fill(int[][] data, int value)
   {
     for (int i = 0; i < data.length; i++)
     {
       for (int j = 0; j < data[i].length; j++)
         data[i][j] = value;
     }
  }

   public static void main(String[] args)
   {
     int[][] months = {{31, 28}, {31, 30, 31}, {30, 31, 31}};
     fill (months, 8);
   }
}


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.
7
Task
New Java Syntax,  level 7lesson 2
Locked
Correct order
Let's write a utility for working with arrays. The main functionality is ready: the printArray() method displays all the elements of the array on the console. What is left for you is just a trifle: implement the reverseArray() method. It should reverse the order of the elements in the array. The met

Methods with the same name

"Now let's return to method names once again."

"I can't imagine what else could be said about names!"

"Well, the Java language standard requires all methods in the same class to have unique names.

"So, it's impossible to declare two identically named methods in the same class?"

"Now — pay close attention! Methods in a class can indeed have identical names! But in this case, they must have different parameters. In other words, methods are compared for sameness, not only are the names are taken into account, but also the types of the parameters! Note that I specifically said types. The names of the parameters are not taken into account. Examples:

Code Explanation
void fill(int[] data, int value) {
}
void fill(int[][] data, int value) {
}
void fill(int[][][] data, int value)  {
}
These three methods are different methods. They can be declared in the same class.
void print(String str) {
}
void print(String str, String str2) {
}
void print(int val) {
}
void print(double val) {
}
void print() {
}
Each of these five methods is considered different. They can be declared in the same class.
void sum(int x, int y) {
}
void sum(int data, int value) {
}
"These two methods are considered the same, meaning they cannot be declared in the same class."

"I'm totally confused! Why do we need all of this? 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. It has no idea what name the programmer gave to the parameter when declaring the method."

Ahh, it seems to be starting to get through!

"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. Programmers need them, not the compiler.

"And I guess that's why they are not taken into account when determining the uniqueness of a method?"

"Yes, that is entirely correct. The name of a method and the types of its parameters are called the method signature. For example, sum (int, int)"

"So each class must have methods with unique signatures rather than methods with unique names."

"Well done, Amigo! You summarized this lesson perfectly. If something remains unclear, don't panic. This topic will become clear after a couple of tasks."

7
Task
New Java Syntax,  level 7lesson 2
Locked
A method for all seasons
It would be nice to have one method for solving all sorts of problems. You have the opportunity to write one: Create 9 more universalMethod() methods. In total, there should be 10 of them. Think of the parameters they should have.