Method to describe by dividing into multiple methods How to pass arguments How to use return value Method overload Pass by value and pass by reference

Benefits of writing separate methods

By dividing the method for each function other than the main method
Improves the visibility of the entire program and makes it easier to correct errors # Method definition
public static return value method name(Argument data type Variable name){
The process that is executed when the method is called
}
・ The inside of {} is called a method block

Method call

(method call method) `Method name (argument list);`

(Example)

 public static void maiu(String[] args){
   System.out.println("Call the method");
   method();
   System.out.println("The method call is complete!");
 }

-No matter how many methods are defined, the main method is always executed first

How to pass arguments

(How to receive arguments) `public static void hello (variable data type variable name);`

(Example)

public static void main(String[] args){
 hello("A");
 hello("B");
 hello("C");
}

public static void hello(String name){
 System.out.println("Hello"+name+"Mr.");
}

(Output result)

Hi A's
Hello Mr. B
Hi C's
-Arguments are automatically assigned to variable names

When passing multiple arguments

(argument assignment method) `Method name (argument 1, argument 2);`
(How to receive arguments) `public static return value method name (argument data type argument 1, argument data type argument 2);`

(Example)

public class li5_5 {
   public static void main(String[] args){
    add("Hello","Mr. A");
    add("Good evening","Mr. B");
   }
   public static void add(String x,String y) {
	System.out.println(x+y);
   }
}

(Output result)

Hi A's
Good evening B
The value to be passed is actual argument and the value to be received is formal argument If the
the above example, "Hello", "A's" "Good evening", "B's" is, in order to correspond to the pass value actual argument. Formal argument because
String x, String y is receiving a value.

How to use the return value

(How to return the return value)
public static Return type Method name(argument){
The process that is executed when the method is called
return Return value;
}
Return type void means no return  
(How to call the method name and receive the return value) `Type variable name = method name (argument list)`  
(Example) * Ignore the lack of sense of word choice

public class li5_7 {
  public static String Hello(String x,String y) {
	String aisatsu = x + y;
	return aisatsu;
  }

  public static void main(String[] args) {
	String aisatsu = Hello("Good morning","Yo");
	System.out.println(aisatsu);
  }
}
(output result) ``` Good morning ```

Use the return value directly

(idea)
In the above example, after receiving the value as a variable once with String aisatsu = Hello ("Oha", "Yo"); in the main method, System.out You are using the return value in .println (aisatsu); .
The following code does not receive in the variable in the main method, but uses the direct return value in the statement.  
(Example) * Please let me know if there is a fashionable example other than good morning
public class li5_8 {
  public static String add(String x,String y) {
	String ans = x + y;
	return ans;
  }

 public static void main(String[] args) {
    System.out.println(add(add("O","Is"),add("Yo","U")));
 }
}
(Supplement) In
add (add ("o", "is"), add ("yo", "u") , the first add ("o", "is")) You can "good morning" with , and "you" with the next add ("yo", "u") . I can.
You can "Good morning" with the add that is summarized at the end.  
(output result) ``` Good morning ``` # Notes on the return statement
(idea)
return causes the method to exit  
(example)
public class li5_8 {
  public static String add(String x,String y) {
	String ans = x + y;
	return ans;
    System.out.println(ans);
  }
}

(Supplement)
System.out.println (ans); results in a compile error

Method overload

(idea)
Only one method with the same name can be used. However, there are cases where multiple methods of similar processing are used. In such a case, you can use multiple methods with the same name by changing the data type of the argument or changing the number of data of the argument .  
(Example ~ When changing the data type of the argument ~)

  public static int add(int x,int y) {
	return x + y ;
  }
  
  public static double add(double x, double y) {
	return x + y ;
  }
  
  public static String add(String x, String y) {
	return x + y ;
  }
  
  public static void main(String[] args) {
    System.out.println(add(10,20));
    System.out.println(add(3.5,2.7));
    System.out.println(add("Hellow","World"));
  }
(output result)
30
6.2
HellowWorld

――――――――――――――――――――――――――――――――――――――――――――――――――――――――――

(Example ~ When changing the number of argument data ~)

  public static String hello(String x,String y) {
	return x + y ;
  }

  public static String hello(String x,String y,String z) {
	return x + y + z ;
  }

  public static void main(String[] args) {
    System.out.println(hello("Good morning","Yo"));
    System.out.println(hello("O","Is","Yo"));
  }
(output result)
Good morning
Good morning

Pass by value and by reference

(idea) Normal variables are passed by value , so even if the source value changes, the destination value does not change. When a array is passed, it is passed by reference , so if the passing source value changes, the passing destination value also changes.  
(Example ~ Normal variable ~)

public class li5_13_1 {
	public static void method1(int x) {
		System.out.println(x);
		x = 20;
	}

	public static void main(String[] args) {
		int x = 10;
		method1(x);
		System.out.println(x);
	}
}
(output result)
10
10
(Supplement) The value 10 is assigned from the `main method` to the` method1 method`. The value is changed with `x = 20;` at the end of `method1 method`, but the output of` main method` remains 10.   ――――――――――――――――――――――――――――――――――――――――――――――――――――――――――
(Example ~ Array variable ~)

public class li5_13_2 {
    public static void incArray(int[] array){
    		for(int i=0 ;i<array.length ;i++) {
                array[i]++;
    		}
    }

    public static void main(String[] args) {
    		int[] array = {1,2,3};
    		incArray(array);
    		for(int i :array) {
                System.out.println(i);
    		}
    }
}

(output result)
2
3
4

(Supplement) Since it is an array variable, the reference address is assigned from the `main method` to the ʻinArray method` instead of the reference value . In the ʻinArray method`, 1 is added to the contents of the `array variable array`. Since it is an array variable, the data stored at the referenced address has been changed. That is, the original data is being rewritten. Therefore, in the ʻinArray method`, the value is not returned to the` main method` by `return`, but the output result of the` main method` is the value after the ʻinArray method` processing. From the above, in the case of array variables, an image that shares the original data .

Recommended Posts