Java address or Pass value

Source: Internet
Author: User

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? 
  1. Package luojing;
  2. Public class Stringdemo
  3. {
  4. public static void Main (String[]args)
  5. {
  6. String str=new string ("Hello");
  7. //Call function to change the value of STR
  8. Change (str);
  9. System.out.println (str);
  10. }
  11. public static void Change (String str1)
  12. {
  13. str1+="luojing";
  14. }
  15. }

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? 
  1. Public class Stringdemo
  2. {
  3. public static void Main (String[]args)
  4. {
  5. StringBuffer str=New StringBuffer ("Hello");
  6. //Call function to change the value of STR
  7. Change (str);
  8. System.out.println (str);
  9. }
  10. public static void Change (StringBuffer str1)
  11. {
  12. Str1.append ("luojing");
  13. }
  14. }


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? 
  1. Public class Test
  2. {
  3. public static void Main (string[] args)
  4. {
  5. Demo demo=New demo ("Hello");
  6. //Call function The value of the variable demo.name
  7. Change (demo);
  8. System.out.println (Demo.getname ());
  9. }
  10. public static void change (Demo d)
  11. {
  12. D.setname ("luojing");
  13. }
  14. }
  15. Class Demo
  16. {
  17. private String name;
  18. Public Demo (String s)
  19. {
  20. Name=s;
  21. }
  22. Public String getName ()
  23. {
  24. return name;
  25. }
  26. public void SetName (String str)
  27. {
  28. NAME=STR;
  29. }
  30. }

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? 
  1. Package luojing;
  2. Public class Test
  3. {
  4. public static void Main (string[] args)
  5. {
  6. Demo demo=New demo ("Hello");
  7. //Call function The value of the variable demo.name
  8. Change (demo);
  9. System.out.println (Demo.getname ());
  10. }
  11. public static void change (Demo d)
  12. {
  13. Demo d1=New demo ("Hello java");
  14. D=D1;
  15. }
  16. }
  17. Class Demo
  18. {
  19. private String name;
  20. Public Demo (String s)
  21. {
  22. Name=s;
  23. }
  24. Public String getName ()
  25. {
  26. return name;
  27. }
  28. public void SetName (String str)
  29. {
  30. NAME=STR;
  31. }
  32. }

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.