[Java] The word passing by reference is bad!

Always stumble Java reference value passing

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.

How to pass general method arguments

・ By value ・ Pass by reference -Pass by reference value

How to pass method arguments in Java

・ By value -Pass by reference value

Example of passing by 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);
}

result

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.

Primitive type

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.

Example of passing by reference value

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");

}

result

// 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

Try to illustrate

参照値渡し.png

I will explain

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.

Summary

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

[Java] The word passing by reference is bad!
The story of not knowing the behavior of String by passing Java by reference
Java is the 5th day
Where is the Java LocalDateTime.now () timezone?
Java pass by value and pass by reference
Java "pass by reference" problem summary
Java reference to understand in the figure
What is the best file reading (Java)
What is the main method in Java?
The Java EE Security API is here!
The story received by Java SE11 silver
What is the Java Servlet / JSP MVC model?
The intersection type introduced in Java 10 is amazing (?)
What does passing function parameters by reference mean?
[Java] Judgment by entering characters in the terminal
[Java] Something is displayed as "-0.0" in the output
What is java
Java8 method reference
What is Java <>?
What is Java
Learn the meaning of "passing the PATH" by building a Java development environment on Mac
java8 method reference
JAVA reference materials
My Java reference
Read the packet capture obtained by tcpdump in Java
The comparison of enums is ==, and equals is good [Java]
[JUnit5] Dealing with "the reference of assertEquals is ambiguous"
Which is better, Kotlin or Java in the future?
Whether Java arguments are passed by value or by reference
Credentials referenced by the AWS SDK for Java by default
What is the difference between Java EE and Jakarta EE?
The story that .java is also built in Unity 2018