In Java, the method of passing a method call parameter is to pass a value, although it is a reference value rather than an object value. (Does Java pass by reference or pass by value ?)

Source: Internet
Author: User
Document directory
  • About the author

Address: http://www.javaworld.com/javaworld/javaqa/2000-05/03-qa-0526-pass.html

In Java, all object variables are referenced, and Java Manages objects through references. However, when passing parameters to a method, Java does not use the reference transfer method, but uses the value transfer method. For example, the badswap () method below:
public void badSwap (int var1, int var2)
{
  int temp = var1;
  var1 = var2;
  var2 = temp;
}
When the badSwap method is used, the original values of var1 and var2 will not change. Even if we use other Object types instead of int, there will be no change, because Java also uses the method of passing values when passing references. (Translator's Note: This is the key, the core of the full text is: 1. Object variables in Java are references 2. Methods in Java are passed by value 3. When passing parameters in a method, the passed value is passed)

If the translator's comments are not clear, it does not matter, look at the following code:

public void tricky (Point arg1, Point arg2)
{
  arg1.x = 100;
  arg1.y = 100;
  Point temp = arg1;
  arg1 = arg2;
  arg2 = temp;
}
public static void main (String [] args)
{
  Point pnt1 = new Point (0,0);
  Point pnt2 = new Point (0,0);
  System.out.println ("X:" + pnt1.x + "Y:" + pnt1.y);
  System.out.println ("X:" + pnt2.x + "Y:" + pnt2.y);
  System.out.println ("");
  tricky (pnt1, pnt2);
  System.out.println ("X:" + pnt1.x + "Y:" + pnt1.y);
  System.out.println ("X:" + pnt2.x + "Y:" + pnt2.y);
}
The output of executing main () is as follows:

X: 0 Y: 0
X: 0 Y: 0
X: 100 Y: 100
X: 0 Y: 0
This method successfully changed the value of pnt1, but the exchange of pnt1 and pnt2 failed! This is the most confusing part of the Java parameter passing mechanism. In main (), pnt1 and pnt2 are references to Point objects. When pnt1 and pnt2 are passed to tricky (), Java uses the method of passing values, passing these two references to arg1 and arg2. In other words, arg1 and arg2 are the replication of pnt1 and pnt2, and the objects they point to are the same. For details, please see the icon below:

Figure 1. After being passed as a parameter, the object has at least two references to itself

Java copies and passes the reference by value, not the object. Thus, method manipulation will alter the objects, since the references point to the original objects. But since the references
are copies, swaps will fail. As Figure 2 illustrates, the method references swap, but not the original references. Unfortunately, after a method call, you are left with only the unswapped original references. For a swap to succeed outside of the method call ,
we need to swap the original references, not the copies.

In main (), the reference is copied and passed by value, and the object itself is not passed. Therefore, the object pointed to by pnt1 in the tricky () method has changed. Because the copy of the reference is passed, the exchange of references can neither cause the exchange of objects, nor change the original reference. As shown in 2, tricky () swaps arg1 and arg2, but does not affect pnt1 and pnt2. Therefore, if you want to exchange the original references pnt1 and pnt2, you can't do it by calling a method.

Figure 2. Only the references as parameters are exchanged, but the original references are unchanged

to sum up:

1. Object variables in Java are references

2. Methods in Java are passed by value

3. When passing parameters in a method, the reference value is passed

The original text is as follows:

Java does manipulate objects by reference, and all object variables are references. However, Java doesn't pass method arguments by reference; it passes them by value.

Take the badSwap () method for example:

public void badSwap (int var1, int var2)
{
  int temp = var1;
  var1 = var2;
  var2 = temp;
}
When badSwap () returns, the variables passed as arguments will still hold their original values. The method will also fail if we change the arguments type fromint toObject, since Java passes object references by value
as well. Now, here is where it gets tricky:

public void tricky (Point arg1, Point arg2)
{
  arg1.x = 100;
  arg1.y = 100;
  Point temp = arg1;
  arg1 = arg2;
  arg2 = temp;
}
public static void main (String [] args)
{
  Point pnt1 = new Point (0,0);
  Point pnt2 = new Point (0,0);
  System.out.println ("X:" + pnt1.x + "Y:" + pnt1.y);
  System.out.println ("X:" + pnt2.x + "Y:" + pnt2.y);
  System.out.println ("");
  tricky (pnt1, pnt2);
  System.out.println ("X:" + pnt1.x + "Y:" + pnt1.y);
  System.out.println ("X:" + pnt2.x + "Y:" + pnt2.y);
}
If we execute this main () method, we see the following output:

X: 0 Y: 0
X: 0 Y: 0
X: 100 Y: 100
X: 0 Y: 0
The method successfully alters the value of pnt1, even though it is passed by value; however, a swap ofpnt1 andpnt2 fails! This is the major source of confusion. In themain () method, pnt1 andpnt2
are nothing more than object references. When you passpnt1 andpnt2 to thetricky () method, Java passes the references by value just like any other parameter. This means the references passed to the method are actuallycopies
of the original references. Figure 1 below shows two references pointing to the same object after Java passes an object to a method.

Figure 1. After being passed to a method, an object will have at least two references

Java copies and passes the reference by value, not the object. Thus, method manipulation will alter the objects, since the references point to the original objects. But since the references are copies, swaps will fail. As Figure 2 illustrates, the
method references swap, but not the original references. Unfortunately, after a method call, you are left with only the unswapped original references. For a swap to succeed outside of the method call, we need to swap the original references, not the copies.

Figure 2. Only the method references are swapped, not the original ones

About the author
Tony Sintes is a principal consultant at BroadVision. Tony, a Sun-certified Java 1.1 programmer and Java 2 developer, has worked with Java since 1997. O'Reilly'sJava in a Nutshell by David Flanagan (seeResources)
puts it best: "Java manipulates objects 'by reference,' but it passes object references to methods 'by value.'" As a result, you cannot write a standard swap method to swap objects.

Related Article

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.