[Java] method

Method invocation and definition

: sunny: A collection of multiple sentences, which are named as a process.

Example.java


public class Main {
  public static void main(String[] args) {
    System.out.println("Teacher: Good morning, everyone ♪");
    student();
    System.out.println("Teacher: Let's study hard today as well ♪");
  }
    
  public static void student() {
    System.out.println("Student: Good morning");
  }
}

public static ~ starts public static void main(String[] args) { 〜 } When public static void student() { 〜 } There are two cases. This is the so-called method. I'll explain it step by step: smile:

3rd line System.out.println ("Teacher: Minasa-n, good morning ♪"); Is output.

4th line student(); Here is the method: smile: In a little more detail, it's a method call. student is the method name. () Will be explained later, so please go through: smile: You can call the method by using the method name (), and you can call the same method name. In this case, the 7th line public static void student() { Call: smile:

8th line System.out.println ("Student: Good morning"); Is output. Until now, the process ends here, but in the case of a method, it returns to the main method. So the next step is on line 5.

5th line System.out.println ("Teacher: Let's study hard for one day today ♪"); Is output.

console.


Teacher: Good morning, everyone ♪
Student: Good morning
Teacher: Let's study hard today as well ♪

As an image Process from the 1st line to the 3rd line. ↓ On the 4th line, cast the magic to call the Majin method. ↓ Then, the called Majin method will do the requested command in a straightforward manner (lines 7-9). lamp_majin.png ↓ The Majin Method, which has finished its errands, returns quickly. ↓ Since the Majin method did it halfway, I restarted the process from the continuation (5th line) and finished it.

An image like ...? : sweat_smile: If you shout out the method name, it will be summoned and processed, and when you're done, you'll be back ... like: sweat_smile: I got that image: sweat_smile:

Let's sort out a little here: smile: ~~ How to call the demon ~~ The method to call the method is as follows.

.


Method name ()

~~ How to create a demon ~~ ** The method to create a method is called method definition **, and the definition method is as follows.

.


public static void method name() {
Statement
}

Of course, ** If the defined method name and the method name to be called do not match, it cannot be called. ** ** By the way, what if you just define the method and don't call it? : thinking:

Example.java


public class Main {
  public static void main(String[] args) {
    System.out.println("Teacher: Good morning, everyone ♪");
    System.out.println("Teacher: Let's study hard today as well ♪");
  }
    
  public static void student() {
    System.out.println("Student: Good morning");
  }
}

console.


Teacher: Good morning, everyone ♪
Teacher: Let's study hard today as well ♪

Students no longer reply: cry: In other words, there is no point in defining it unless you call it. This is the state because the Majin method is not called either. ↓ lamp.png

Here I will tell you the advantage of separating the processing by method: smile:

Using the: sunny: method makes the program easier to read and understand the whole thing. : sunny: Makes it easier to fix problems when they occur. The: sunny: method can be used repeatedly, eliminating the need to write the same process over and over again, saving you the trouble of writing code.

This time, I didn't feel the benefit of calling the method because there was only one line of processing, but I think the more code you have, the more you can benefit from it.

public static void main(String[] args) { } : sunny: Whenever you run a Java file ** the main method is automatically executed. ** ** : sunny: main method gives instructions to each method, and each supported method performs individual processing

The octopus that has been a mystery until now. ↓ public static void main(String[] args) { }

I had no idea what it meant, and I remembered it like a sutra: sweat_smile: Apparently, the program is executed by filling in words like this sutra. In other words, if you do not write it, no program will be executed: sweat_smile:

It seems that this main method is the leader and gives instructions to other methods.

What should I do so far? I thought, but it was pretty important: sweat_smile:

Example.java


public class Main {
    public static void student() {
        System.out.println("Student: Good morning");
    }
    
	public static void main(String[] args) {
        System.out.println("Teacher: Good morning, everyone ♪");
        student();
        System.out.println("Teacher: Let's study hard today as well ♪");
    }
}

If you process the main method first, it will be read from the main method even if you put the main method at the bottom as described above, right? ?? : thinking:

console.


Teacher: Good morning, everyone ♪
Student: Good morning
Teacher: Let's study hard today as well ♪

It was loaded properly! : raised_hands:

Call a method from other than the main method

Example.java


public class Main {
    public static void main(String[] args) {
        methodA();
    }
    
    public static void methodA() {
        System.out.println("Method A:Method B");
        System.out.println("Method A:This is method A");
        System.out.println("Method A:Please do this processing");
        methodB();
    }
    
    public static void methodB() {
        System.out.println("Method B:Yes, method B");
        System.out.println("Method B:I understand");
        System.out.println("Method B:Processing");
    }
}

console.


Method A:Method B
Method A:This is method A
Method A:Please do this processing
Method B:Yes, method B
Method B:I understand
Method B:Processing

The first starting point is the main method Other method → I was able to send the process to another method: smile:

Majin Method A called Majin Method B by himself and asked for processing: smile:

Argument (Hikisu)

: sunny: Something like additional information given to the method If you pass an argument with the: sunny: method when you call it, you can use that value in the method.

I think there was () when defining and calling the method earlier. The argument uses this (): smile:

I think it's faster to see how to use it, so enter the code.

Example.java


public class Main {
    public static void main(String[] args) {
        like("Is it");
    }
    
    public static void like(String animal) {
        System.out.println("My favorite animal is" + animal + "is");
    }
}

I wish I could explain it well, but I can't seem to explain it well, so I'll ask you to appear in the Majin method: sweat_smile:

You will appear in the Majin Method with the magic of like on the third line. At that time, write what to pass to the Majin method in (). This time, we will pass whether it is or not.

The Majin method that receives the message confirms the fifth line. On the 5th line

like(String animal)

Was written. The Majin method looks at the contents of () and discovers that it is written as String. Here, we recognize that this is a character. And if the code says animal, it's a good idea. I understand.

Found animal on line 6: raised_hands: Soore, Buchikome: fist:

console.


Is there any animal I like

Will be: smile:

I think it's confusing because it seems to be a mess of methods, arguments, variables, and so on. I have little understanding when thinking only with letters, so it is easy to deepen my understanding because it is easy to make an impression when anthropomorphizing or thinking with a little story and thinking interestingly: smile:

1.like ("ka"); ← Call a demon and give him only his belongings. 2.like (String animal) ← The devil confirms his belongings, confirms the data type (String), and waits for the animal (variable) 3. My favorite animal is "+ animal +" ← Kita! Buchikome!

Like: sweat_smile:

By the way

public static void student() { When student();

So, I think that no argument was written. ** Even if there is no information to pass, () is necessary Please be sure to fill it out. ** **

Describes how to call and define the argument: smile:

Argument call.


Method name (argument)

Method definition (argument).


public static void method name(Type variable) {
Statement
}

Pass multiple arguments

: sunny: Just separate by (comma)

Example.java


public class Main {
    public static void main(String[] args) {
        myself("Miki", 5);
    }
    
    public static void myself(String name, int birth) {
        System.out.println("my name is" + name + " " + birth + "Born on the moon");
    }
}

Let the my method on the third line have the information Miki and 5 as arguments. Confirm that Miki assigns to name with a character (String) on the 5th line. And confirm that 5 is an integer (int) and is assigned to birth. Since there were name and birth on the 6th line, substitute them respectively.

console.


My name is Miki was born in May

If you want to have multiple pieces of information, it's very easy because you just separate them with ,, smile: But there is one caveat: point_up:

myself ("Miki", 5); public static void myself(int birth, String name)

Is NG: no_good: Even though I passed it in the order of Miki (character), 5 (integer) If the receiving side is in the order of int (integer) and String (character), it will not match and a compile error will occur.

By the way, the arguments passed to the ** method are called actual arguments ** ("Miki", 5) ** The recipient is called a formal argument ** (String name, int birth)

Return value

: sunny: Returns a value from the called method to the calling method.

Example.java


public class Main {
    public static void main(String[] args) {
        int answer = number(10, 20);
        System.out.println("The answer is" + answer);
    }
    
    public static int number(int x, int y) {
        int sum = x + y;
        return sum;
    }
}

3rd line int answer = number(10, 20); First call the method number. Pass 10 and 20 to method number.

6th line public static int number(int x, int y) The third int of. I think it would have been void all the time. This has to do with the return value: smile: I'll leave the explanation here for a moment and move on to: smile:

7th line int sum = x + y; Line 6 passes 10 and 20 as number (int x, int y). In other words x = 10 y = 20 So the answer 30 of 10 + 20 is assigned to the variable sum.

8th line return sum; return 30 ・ ・ ・ In other words, 30 is returned to number (10, 20); of the main method.

3rd line int answer = number(10, 20); 30 was assigned to number (10, 20); and it came back. In other words int answer = 30 30 is assigned to answer.

4th line The answer is displayed as 30.

console.


The answer is 30

I will explain the 6th line earlier. public static int int. This represents the return type. This return value is the sum of the return sum on the 8th line. 30 is assigned to sum. That is an integer. The integer data type is int. That's why it's a public static int: smile:

By the way, void that I have been using so far. This ** void means no return value **. If there is no return value ... void If there is a return value ... Return type That means: smile:

Call method, receive return value (return value).


Type variable name=Method name (argument list);

Method definition (return value).


public static return type method name(Argument list) {
Statement
return Return value
}

There is one caveat when using return. return not only returns the value, but also terminates the ** method. ** ** If you write code after return, it will not be executed. A compile error will occur.

Use the return value as it is

Earlier we used variables to return the return value. But you can use it as is: smile:

Example.java


public class Main {
    public static void main(String[] args) {
        System.out.println("The answer is" + sum(10, 20));
    }
    
    public static int sum(int x, int y) {
        int answer = x + y;
        return answer;
    }
}

console.


The answer is 30

Overload

: sunny: Define a method with the same name.

Basically, you can't give the same name to a method, but you can define more than one if the type and the number of arguments are different: smile:

Example.java


public class Main {
    public static void main(String[] args) {
        fruit("Apple");
        fruit("Apple", "Mandarin orange");
    }
    
    public static void fruit(String name) {
        System.out.println("this is" + name + "is");
    }
    
    public static void fruit(String name1, String name2) {
        System.out.println("this is" + name1 + "When" + name2 + "is");
    }
}

console.


This is an apple
This is an apple and a mandarin orange

It was recognized properly: smile: Although fruits with the same method name Because the number of arguments is different, the computer (JVM) recognizes that it is different because the name is the same but the number of arguments is different: smile:

Use an array as an argument

Example.java


public class Main {
	public static void main(String[] args) {
		String[] fruits = {"Apple", "Mandarin orange", "Strawberry"};
		food(fruits);
	}

	public static void food (String[] fruits) {
		for (String element : fruits) {
			System.out.println(element);
		}
	}
}

The third line creates the array fruits. Call the food method on line 4. The for statement on the 7th line turns the array and outputs all the contents of the array.

console.


Apple
Mandarin orange
Strawberry

Will be: smile: As a note public static void food (String[] fruits) Don't forget ** [] after the String in ** Maybe: smile:

Recommended Posts

Java method
java (method)
Java method
[Java] method
[Java] method
Java8 method reference
[Java] forEach method
java8 method reference
[Java] Random method
[Java] split method
Method
Java learning 2 (learning calculation method)
Java learning memo (method)
About Java method binding
[Java ~ Method ~] Study memo (5)
About method splitting (Java)
Studying Java 8 (see method)
Java
Java
[Java] Basic method notes
[Java] New Thread generation method (2)
Java GC method determination conditions
Java Silver Study Method Memo
Create a java method [Memo] [java11]
[Java Silver] About equals method
[Java] Timer processing implementation method
[Java] Random number generation method (Random)
Java methods and method overloads
Benefits of Java static method
[Java Silver] Array generation method
[Java] New Thread generation method (1)
Java learning (0)
Studying Java ―― 3
[Java] array
Java protected
[Java] Annotation
[Java] Module
Java array
Studying Java ―― 9
Java scratch scratch
Java tips, tips
Java methods
java (constructor)
to_i method
Java array
java (override)
[Java] Object-oriented syntax --class method / argument
Java Day 2018
Java string
Java static
Java serialization
java beginner 4
JAVA paid
Automatic photo resizing method in Java
getRequestDispatcher () method
Studying Java ―― 4
Java (set)
merge method
[Java] compareTo
Studying Java -5
Java method list (memorial) (under construction)