Original link: http://blog.csdn.net/jdluojing/article/details/6962893
Java is the transfer of value or address, this issue has been discussed for a long time, some people say is the value of, some people say to look at the situation, some cases are transmitted value, some cases are transmitted. Is it a value or a pass? For the time being, we will first interpret the communication as a C + + pointer.
First look at the following procedure:
[Java] view plaincopyprint?
- Package luojing;
- Public class Stringdemo
- {
- public static void Main (String[]args)
- {
- String str=new string ("Hello");
- //Call function to change the value of STR
- Change (str);
- System.out.println (str);
- }
- public static void Change (String str1)
- {
- str1+="luojing";
- }
- }
Program execution Result: Hello
You can see that the value of STR does not change, although the string type is an object, but it is a constant, so the address is equivalent to the constant pointer const int*p=a; we can change the direction of P, but we cannot change the contents of *p (that is, a). In Java, when we change the value of the string constant str, we actually create a new string object, and then assign the address of the new object to str1, so we just change the address that the str1 points to, and it does not change the content that str1 originally pointed at the address. And when we address the change () parameter, we pass a copy of the STR, which is the equivalent of passing a new pointer str1 to the address pointed to by STR, and we are changing the change () function just the pointer we passed STR1, There is no effect on the true str address. So when we output STR, its value is still hello.
We will now replace the string in the above program with StringBuffer to try it out:
[Java] view plaincopyprint?
- Public class Stringdemo
- {
- public static void Main (String[]args)
- {
- StringBuffer str=New StringBuffer ("Hello");
- //Call function to change the value of STR
- Change (str);
- System.out.println (str);
- }
- public static void Change (StringBuffer str1)
- {
- Str1.append ("luojing");
- }
- }
Program run result : Hello luojing This time the content of STR changed.
We'll replace the above stringbuffer with our own defined objects:
[Java] view plaincopyprint?
- Public class Test
- {
- public static void Main (string[] args)
- {
- Demo demo=New demo ("Hello");
- //Call function The value of the variable demo.name
- Change (demo);
- System.out.println (Demo.getname ());
- }
- public static void change (Demo d)
- {
- D.setname ("luojing");
- }
- }
- Class Demo
- {
- private String name;
- Public Demo (String s)
- {
- Name=s;
- }
- Public String getName ()
- {
- return name;
- }
- public void SetName (String str)
- {
- NAME=STR;
- }
- }
Program Run Result: luojing and we use StringBuffer object is the same effect.
Let's make some changes to the change () method:
[Java] view plaincopyprint?
- Package luojing;
- Public class Test
- {
- public static void Main (string[] args)
- {
- Demo demo=New demo ("Hello");
- //Call function The value of the variable demo.name
- Change (demo);
- System.out.println (Demo.getname ());
- }
- public static void change (Demo d)
- {
- Demo d1=New demo ("Hello java");
- D=D1;
- }
- }
- Class Demo
- {
- private String name;
- Public Demo (String s)
- {
- Name=s;
- }
- Public String getName ()
- {
- return name;
- }
- public void SetName (String str)
- {
- NAME=STR;
- }
- }
Run result:Hello
As you can see, although we have changed d in the change () method, the actual object demo has not changed.
This shows that theso-called "address" in Java is actually a value (or not in Java). When we "pass" the method, we are actually passing a copy of the address of the actual argument, which is the same as the address that our arguments (which we interpret as a pointer here for the moment), but they are two different entities. So when we re-assign a parameter to a method, it changes only the address that the parameter points to, and the address that the argument points to is not changed, so its contents are unchanged.
Java address or Pass value