Location>code7788 >text

Value Passing and Reference Passing in Java

Popularity:866 ℃/2024-08-15 22:41:06

In the information that can be searched on the Internet, the discussion about whether Java is value passing or reference passing is more than enough, and there is not a particularly recognized conclusion.

Because I've moved to Golang development in the last year or two, I've been exposed to more pointer play, and suddenly I've become somewhat interested in Java's reference passing and value passing again.

But I don't intend to discuss whether Java is value passing or reference passing, I'm just documenting it to avoid stepping on potholes when developing in the future.

To verify, I wrote this code:

private void transInt(int x) {
        x += 100; }
}

// This code is called with the following logic:
int x = 10; (x);
(x);
("-----------------after int trans----------------");
(x);

This code unsurprisingly prints 10, and from this point on, Java is value-passing, because what is passed into transInt is a copy of x.

Things don't end that simply though, the above example is too specific and I'm using the basic types provided by Java.

Whether this is still true when switching to a String type is a matter of validation of the value, so a piece of code like this should be implemented:

private void transString(String x) {
   x += "bar"; }
}

// This code is called with the following logic:
String str = "foo";
(str);

The result of this code is "foo", which means that non-basic types are also passed as values.

Things at this point it seems to be possible to say that Java is value-passing, but things do not end so simply, I implemented a class:


class Solution implements Cloneable {
    private int age;

    private String name;

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
         = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
         = name;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return (Solution) ();
    }
}

Then a method is implemented:

    private void transfer(Solution solution) {
        (12);
        ("lee").
    }

// Call the method as follows:
Solution solution = new Solution();
("foo").
(20);
("--------------before transfer------------------------");
(()));
(()); ("before transfer"); (()); (()).


(solution).

("--------------after transfer-------------------").
(()).
(()).

The printout is as follows:

--------------before transfer------------------------
20
foo
--------------after transfer-------------------
12
lee

This result is clearly reference passing. It is now possible to say that Java is reference passing again.

So you can not simply say that Java is value passing or reference passing, the water here is still relatively deep, if you do not know very well, it is likely that some strange problems in the code implementation.

At this point I think Java steals a bit more, similarly if it's Golang, it's more straightforward.

Take this code for example:

package main

import "fmt"

type Student struct {
	Name string
	Age  int
}

func transStu(stu Student) {
	 = 12
	 = "bar"
}

func transStuPoint(stu *Student) {
	 = 100
	 = "lee"
}

func main() {
	stu := &Student{
		Name: "foo",
		Age:  21,
	}

	("%s:%d\n", , )

	transStu(*stu)
	("%s:%d\n", , )

	transStuPoint(stu)
	("%s:%d\n", , )
}

If a value is passed in, then it's a value pass, and if a pointer is passed in, then it's a reference pass, and control is given to the programmer, so this code prints:

foo:21
foo:21
lee:100

When I was learning C before, I switched to Java because I couldn't stand such flexible pointers, but now it seems wise for C to give most of the control to the programmer.

Basically, I haven't seen any discussion online about whether C or Go is value-passing or reference-passing.