Whether Java arguments are passed by value or by reference

To be honest, it doesn't matter if you know it or not, but it's about how to pass Java arguments.

Java is often referred to as passing by reference, but I wasn't hungry for some reason I was studying C ++. I felt that the fog had cleared up, so I wrote an article.

Again, it doesn't matter if you know or don't know if you're limited to Java.

For primitive types

	public static void main(String[] args) {

		int a = 1;
		int b = 2;

		System.out.println("Before swap");
		System.out.println("a = " + a);
		System.out.println("b = " + b);

		swap(a, b);

		System.out.println("After swap");
		System.out.println("a = " + a);
		System.out.println("b = " + b);
	}

	static void swap(int x, int y) {
	
		System.out.println("In swap");
	
		int t = x;

		x = y;
		y = t;

		System.out.println("x = " + x);
		System.out.println("y = " + y);
	}

output


Before swap
a = 1
b = 2
In swap
x = 2
y = 1
After swap
a = 1
b = 2

The `swap``` method arguments ``` x, `y``` are copies of the caller ```a```, b``` and are different entities. is. Therefore, swapping the values of `` x and `` `y in the` `` swap``` method does not affect the caller.

This is `passing the value by value`.

For objects

	public static void main(String[] args) {

		Integer c = new Integer(1);
		Integer d = new Integer(2);

		System.out.println("Before swap");
		System.out.println("c = " + c);
		System.out.println("d = " + d);

		swap(c, d);

		System.out.println("After swap");
		System.out.println("c = " + c);
		System.out.println("d = " + d);
	}

	static void swap(Integer x, Integer y) {

		System.out.println("In swap");

		Integer t = x;

		x = y;
		y = t;

		System.out.println("x = " + x);
		System.out.println("y = " + y);
	}

output


Before swap
c = 1
d = 2
In swap
x = 2
y = 1
After swap
c = 1
d = 2

The result is the same as the primitive type, even if the values are swapped in the swap method, the caller's value is not swapped. swapA reference to an instance of the integer class is passed as an argument to the method. However, the reference passed to the argument is a ** copy of the reference ** at the caller, as in the case of primitive types. That is, even if the `swap``` method swaps the references for x``` and `` y, the callers a and `` `b The reference to is unchanged. This means `reference passed by value`.

However, in the case of an object, since the reference is passed as an argument, isn't it okay to pass the reference?

The rest is a digression.

I think that passing a reference in a strict sense is probably a reference in C ++ as an argument.

However, I remember that when I bite C language a little while ago, I used to pass arguments by value and by reference, so I think that passing a C language reference and passing a pointer as an argument. However, strictly speaking, this is also `pointer pass by value`.

To be honest, the context in C language and understanding are ambiguous, but if there is a context of passing `pointer by value` by reference, even Java objects can be called passing by reference. Absent?

Your own conclusion

Strictly speaking, Java is passed by value. But, to say the arguing, it's not wrong to say that it's passed by reference.

What is really important

Whether it's passed by value or by reference, for Java, that reference must be passed when passed to an object argument.

Since the destination of the reference is the same in the method caller and in the method, if the object is manipulated through the reference in the method, the caller may be affected as a side effect.

Recommended Posts

Whether Java arguments are passed by value or by reference
How to change arguments in [Java] method (for those who are confused by passing by value, passing by reference, passing by reference)
Java "pass by reference" problem summary
Whether to use Java Comparable or Comparator
[Java] The word passing by reference is bad!