Java Classic question: value and pass-through?

Source: Internet
Author: User

Transferred from: http://developer.51cto.com/art/201104/254715.htm

Is Java really a value or a reference? It is believed that very few people can answer correctly. The general argument is that for the basic data type (integer, float, character, Boolean, etc.), the value is passed, and for reference types (objects, arrays), a reference is passed. The basic type is passed, and no one has any doubts about it; the problem is on the reference type.

In order to introduce the topic, may wish to take a look at the following example, you can correctly give the results of the program run?

  1. /**
  2. * @ (#) Swap.java
  3. *
  4. *
  5. * @author
  6. * @version 1.00 2007/1/5
  7. */
  8. public class Swap {
  9. Public Swap () {}
  10. public static void Main (string[] args) {
  11. Changer C = new Changer ();
  12. String stra = "Mighty";
  13. String strb = "Mouse";
  14. C.swap (Stra, STRB);
  15. System.out.println (stra + "" + strb);
  16. string[] Strarr = new string[ 2];
  17. strarr[ 0] = stra;
  18. strarr[ 1] = STRB;
  19. C.swap (Strarr);
  20. System.out.println (strarr[ 0] + "" + strarr[ 1]);
  21. }
  22. static class Changer {
  23. Public < T > void Swap (t A, T b) {
  24. T temp = A;
  25. A = b;
  26. b = temp;
  27. }
  28. Public < T > void Swap (t[] t) {
  29. if (T.length < 2) {
  30. System.out.println ( "error!");
  31. return;
  32. }
  33. T temp = t[ 0];
  34. t[ 0] = t[ 1];
  35. t[ 1] = temp;
  36. }
  37. }
  38. }

The correct operation result of the above program is:

Mighty Mouse

Mouse Mighty

Are you correct?

Let's analyze: Why do the above results appear?

To analyze this problem, we must have some knowledge of the layout of the data in the program in memory. There are 3 variables related to string in the main program above, and the layout can be as follows:

When the swap (stra, STRB) function is called, the copy value of the reference type Stra, STRB is passed, so any changes to the parameters in the function do not affect the values of stra and strb, whereas when the swap (Strarr) is called, the Strarr copy value is passed. Any changes to the parameters in the program still do not affect the value of the Strarr, but the change in swap (t[] T) is not the value of Strarr, but the value of strarr[0] and strarr[1, which is the value of the object pointed to by the reference type Strarr, thus strarr[ 0] and the value of strarr[1] has changed.

From the above analysis, we can conclude that, for reference types, arguments are still passed by value, but it is not entirely unreasonable to pass by reference, except that the reference object is not the reference type itself, but rather the object that the reference type points to.

Java Classic question: value and pass-through?

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.