Arguments are like additional information given to a method. If you pass an argument with it when you call the method, its value will be available in the method. To pass an argument to a method, first define a method that can accept the argument. To do this, specify the variable (dummy argument) that receives the argument in the method definition part.
Main.java
public static void method name(Data type variable name) { //Data type variable nameは仮引数です
Process to be executed;
}
[Example]
Main.java
class Main {
public static void main(String[] args) {
nameData("Sato");
nameData("Suzuki");
}
public static void nameData(String name) { //I try to receive arguments
System.out.println("Name is"+name+"is");
}
}
In the above example, nameData is the method and (String name) is the data type variable name. System.out.println ("name is" + name + ""); is the process to perform. As a result, "My name is Sato" and "My name is Suzuki" appear.
In order for the method to receive multiple arguments, define the formal arguments separated by commas (,).
Main.java
public static void method name(Data type variable name,Data type variable name) { //From the left, the first argument and the second argument
Process to be executed;
}
[Example]
Main.java
class Main {
public static void main(String[] args) {
nameData("Sato",20);
nameData("Suzuki",30);
}
public static void nameData(String name,int age) { //I try to receive arguments
System.out.println("Name is"+name+"And the age is"+age+"is");
}
}
The result is "My name is Sato and my age is 20" and "My name is Suzuki and my age is 30".
If you want to use the processing result of the method in the caller of the method, make the method return the return value. For example, if you think of a method as a factory, the factory (method) receives materials (arguments) from the orderer (referred to as the main method), performs the specified processing (processing), and returns the finished product to the orderer. Think of this finished product as the return value. return You can use return inside a method to return the value of return to the method caller. Also, methods that have a return value specify the data type of the return value. Specify in the void part of "public static void". The void used so far means that there is no return value.
Main.java
public static Return data type Method name(argument) {
return Return value;
}
[Example]
Main.java
public static int main(int a,int b) {
return a + b;
}
In the above example, int is the return data type, main is the method name, int a, int b is the argument, a + b is the return value, and return returns the value to the caller. [Example]
Main.java
class Main {
public static void main(String[] args) {
String name = fullName("Sato", "Taro"); //Assigning the result of the fullName method to the variable name
nameData(name,20); //Replaced the argument of nameData with name.
nameData("Suzuki Antony Jiro", 30);
}
public static void nameData(String name, int age) {
System.out.println(""Name is"+name+"And the age is"+age+"is");
}
//Defines fullName method
public static String fullName(String firstName, String lastName) {
return firstName + " " + lastName;
}
}
The above result is My name is Taro Sato and my age is 20 My name is Jiro Suzuki and my age is 30 It will be.
First, you cannot define the same name for a method. This is because if there are multiple same methods, you will not know which method to use when you call the method. However, if the argument types and numbers are different, you can define a method with the same name. This is because even if there are methods with the same name, if the arguments are different, you can know which one to call. Defining a method with the same name is called overloading.
Main.java
public static void main(String[] args) {
hello(); //Method with no arguments
hello("Sato"); //Method with String type argument
}
Main.java
public static void hello() { //Method with no arguments
System.out.println("Good morning");
}
public static void hello(String name) { //Method with String type argument
System.out.println("Good morning" + name);
}
Since hello and the method have the same name but different arguments, you can define a method with the same name. [Example]
Main.java
class Main {
public static void main(String[] args) {
nameData(fullName("Sato", "Taro"), 20);
nameData(fullName("Suzuki", "Antony", "Jiro"), 30);
}
public static void nameData(String name, int age) {
System.out.println("Name is"+name+"And the age is"+age+"is");
}
public static String fullName(String firstName, String lastName) {
return firstName + " " + lastName;
}
public static String fullName(String firstName, String middleName, String lastName) {
return firstName + " " + middleName + " " + lastName;
}
}
Recommended Posts