I often hear that passing by reference does not exist in Java. At first I thought "Eh", but I summarized the results of my research.
・ By value ・ Pass by reference -Pass by reference value
・ By value -Pass by reference value
RefValHandOver.java
private static void editNumber(int arg){
arg = 0;
System.out.println("editNumber : " + arg);
}
public static void main(String[] args) {
int number = 100;
editNumber(number);
System.out.println( "main : " + number);
}
editNumber : 0
main : 100
It is 0 in editNumber, but it remains 100 in main. If the argument is a primitive type, it will be passed by value.
Mold | bit | initial value |
---|---|---|
boolean | 1bit | false |
byte | 8bit | 0 |
char | 16bit | \u0000 |
short | 16bit | 0 |
int | 32bit | 0 |
float | 32bit | 0.0 |
long | 64bit | 0 |
double | 64bit | 0.0 |
There is no particular problem so far.
I will explain by classifying it into three patterns. ・ 1. When data is manipulated in the method ・ 2. Renew in the method. -3. Substitute null in the method.
RefValHandOver.java
// 1.When data is manipulated in a method
private static void methodAddArray(ArrayList<String> arg){
arg.add("B");
System.out.println("methodAddArray : " + arg.hashCode() + " : " + arg.toString());
}
//2.Renew in the method.
private static void methodNewArray(ArrayList<String> arg){
arg = new ArrayList<String>();
arg.add("C");
System.out.println("methodNewArray : " +arg.hashCode() + " : " + arg.toString());
}
//3.Assign null in the method.
private static void methodNullArray(ArrayList<String> arg){
arg = null;
System.out.println((arg != null) ? "methodNullArray : " + arg.hashCode() + " : " + arg.toString() : "null");
}
public static void main(String[] args) {
ArrayList<String> arrayList = new ArrayList<String>() {{add("A");}};
// 1.When data is manipulated in a method
methodAddArray(arrayList);
System.out.println("main : " + arrayList.hashCode() + " : " + arrayList.toString());
//2.Renew in the method.
methodNewArray(arrayList);
System.out.println("main : " + arrayList.hashCode() + " : " + arrayList.toString());
//3.Assign null in the method.
methodNullArray(arrayList);
System.out.println((arrayList != null) ? "main : " + arrayList.hashCode() + " : " + arrayList.toString() : "null");
}
// 1.When data is manipulated in a method
methodAddArray : 3042 : [A, B]
main : 3042 : [A, B] //The reference destination has not changed
//2.Renew in the method.
methodNewArray : 98 : [C]
main : 3042 : [A, B] //The reference destination has changed
//3.Assign null in the method.
methodNullArray : null
main : 3042 : [A, B] //The reference destination has changed
The focus is on whether you are ___assigning a new reference to the argument. When assigned, the "local variable in main" and the "argument in the method" will be different.
We have described ___passed by value ___ and ___ passed by reference value ___. Personally, I think it would be easier to understand if passing by reference value is ___ passing by reference address ___ </ span>.
Recommended Posts