public static return value method name(Argument data type Variable name){
The process that is executed when the method is called
}
(Example)
public static void maiu(String[] args){
System.out.println("Call the method");
method();
System.out.println("The method call is complete!");
}
(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
(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
"Hello", "A's" "Good evening", "B's" code> is, in order to correspond to the pass value actual argument. strong> dd>
Formal argument because String x, String y code> is receiving a value. strong> dd>
How to use the return value
(How to return the return value) dd>
public static Return type Method name(argument){
The process that is executed when the method is called
return Return value;
}
Return type void code> means no return strong> dd>
(How to call the method name and receive the return value) dd>
`Type variable name = method name (argument list)`
(Example) * Ignore the lack of sense of word choice dd>
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) dd>
```
Good morning
```
Use the return value directly
(idea) dd>
In the above example, after receiving the value as a variable once with String aisatsu = Hello ("Oha", "Yo"); code> in the main method, System.out You are using the return value in .println (aisatsu); code>. dd>
The following code does not receive in the variable in the main method, but uses the direct return value in the statement. dd>
(Example) * Please let me know if there is a fashionable example other than good morning dd>
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) dd>
In add (add ("o", "is"), add ("yo", "u") code>, the first add ("o", "is")) You can "good morning" strong> with code>, and "you" strong> with the next add ("yo", "u") code>. I can. Dd>
You can "Good morning" strong> with the add that is summarized at the end. dd>
(output result) dd>
```
Good morning
```
# Notes on the return statement
(idea) dd>
return code> causes the method to exit dd>
(example) dd>
public class li5_8 {
public static String add(String x,String y) {
String ans = x + y;
return ans;
System.out.println(ans);
}
}
(Supplement) dd>
System.out.println (ans); code> results in a compile error dd>
Method overload
(idea) dd>
Only one method with the same name can be used. strong> However, there are cases where multiple methods of similar processing are used. dd>
In such a case, you can use multiple methods with the same name by changing the data type of the argument strong> or changing the number of data of the argument strong>. dd>
(Example ~ When changing the data type of the argument ~) dd>
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) dd>
30
6.2
HellowWorld
――――――――――――――――――――――――――――――――――――――――――――――――――――――――――
(Example ~ When changing the number of argument data ~) dd>
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) dd>
Good morning
Good morning
Pass by value and by reference
(idea) dd>
Normal variables are passed by value strong>, so even if the source value changes, the destination value does not change. strong>
When a array is passed, it is passed by reference strong>, so if the passing source value changes, the passing destination value also changes. strong>
(Example ~ Normal variable ~) dd>
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) dd>
10
10
(Supplement) dd>
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 ~) dd>
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) dd>
2
3
4
(Supplement) dd>
Since it is an array variable, the reference address strong> is assigned from the `main method` to the ʻinArray method` instead of the reference value strong>.
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 strong>.
Recommended Posts