Java arguments, return values and overloads

argument

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.

Multiple arguments

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

Return value

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.

Method overload

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

Java arguments, return values and overloads
[Java] Arguments and parameters
Java methods and method overloads
[Java] What are overrides and overloads?
Java and JavaScript
XXE and Java
[Ruby] Arguments with keywords and default values of arguments
[Java] Get multiple values from one return value
[Java] Convert and import file values with OpenCSV
[Android / Java] Screen transition and return processing in fragments
Getters and setters (Java)
[Java] Thread and Runnable
Java true and false
[Java] String comparison and && and ||
Formal and actual arguments
Java --Serialization and Deserialization
timedatectl and Java TimeZone
Reverse Enum constants from strings and values in Java
[Java] Branch and repeat
[Java] [Kotlin] Generically call valueOf and values of Enum
[Java] Variables and types
java (classes and instances)
Try Java return value
[Java] Overload and override
JSON in Java and Jackson Part 1 Return JSON from the server
Difficult to handle minimum values for Java8 LocalDateTime and Java6 Date
Get attributes and values from an XML file in Java
Study Java # 2 (\ mark and operator)
Java version 8 and later features
[Java] Difference between == and equals
[Java] Stack area and static area
[Java] Generics classes and generics methods
Java programming (variables and data)
Java encryption and decryption PDF
Java and Iterator Part 1 External Iterator
Java if and switch statements
Java class definition and instantiation
Apache Hadoop and Java 9 (Part 1)
[Java] About String and StringBuilder
[Java] HashCode and equals overrides
☾ Java / Iterative statement and iterative control statement
java Generics T and? Difference
Advantages and disadvantages of Java
java (conditional branching and repetition)
About Java Packages and imports
[Java] Upload images and base64
C # and Java Overrides Story
Java abstract methods and classes
Java while and for statements
Java encapsulation and getters and setters
Management of partitioned values and their names Enum (Java) vs DB
Note No. 1 "Counting and displaying duplicate values in an array" [Java]
I want to return to the previous screen with kotlin and java!