Is the "Doubts" Java method parameter a reference call or a value call?

Source: Internet
Author: User
Tags call by reference

Recently to find an internship, in the written test did the following: To find the output of the following code.

public class MyClass {

static void Amethod (StringBuffer sf1,stringbuffer sf2) {
Sf1.append (SF2);
SF2=SF1;
}

public static void Main (string[] args) {
StringBuffer sf1=new stringbuffer ("A");
StringBuffer sf2=new stringbuffer ("B");
Amethod (SF1,SF2);
System.out.println (sf1+ ":" +SF2);
}
}

The output I was thinking of was: Ab:ab. Obviously fell into the trap of the person in the question, the correct answer should be ab:b.

Because long time did not see Java, may have some knowledge point unfamiliar, this question has tested the Java value call.

In the Java programming language, the use of method parameters should be noted in three points:

A method cannot modify a parameter of a base data type (that is, numeric and Boolean)

A method can change the state of an object parameter

A method cannot be implemented so that the object argument references a new object.

To better understand, I read a blogger's blog, the content is as follows:

Reproduced in: http://hxraid.iteye.com/blog/428856

method Invocation (call by) is a standard term for computer science. Method invocations are also divided into value calls (call by reference) and reference calls (call by value) , depending on the arguments passed. There are a lot of definitions of these two calls, the most common is that the value of the transfer value is called, the delivery address is a reference call. This is very inappropriate, this is very easy to let us think that the Java object parameter passing is a reference call, in fact,thejava object parameter pass is still a value call.  


We first use a piece of code to verify why the object parameter passing of Java is a value call.

Java code
  1. Public class Employee {
  2. Public String name=null;
  3. Public Employee (String N) {
  4. this.name=n;
  5. }
  6. //exchange of two employee objects
  7. public static void Swap (Employee e1,employee E2) {
  8. Employee temp=e1;
  9. E1=e2;
  10. E2=temp;
  11. System.out.println (e1.name+"" +e2.name); //Print Result: Li Shizhang
  12. }
  13. //Main function
  14. public static void Main (string[] args) {
  15. Employee worker=New Employee ("Zhang San");
  16. Employee manager=New Employee ("John Doe");
  17. Swap (Worker,manager);
  18. System.out.println (worker.name+"" +manager.name); //Print results are still: Dick and Harry
  19. }
  20. }

The above results are disappointing, although the parameter object e1,e2 the content exchange, but the argument object Worker,manager is not interchangeable content. The most important reason for this is that the formal parameter e1,e2 is the address copy of the argument Worker,manager.

As you know, in Java, the object variable name actually represents the address of the object in the heap (the jargon is called an object reference ). At the time of the Java method invocation, the argument passes the object's reference. It is important that the memory address of the formal parameter and the argument is not the same, and the content in the formal parameter is only one copy of the object reference stored in the argument.

If you know something about the local variables of the Java stack in the JVM's memory management (see Java Virtual Machine architecture), this is a good thing to understand. When the JVM runs the above program, running the main method and the swap method will successively push two memory spaces called stack frames in the Java stack. The memory in the main stack frame called the local variable area is used to store references to the argument object worker and manager. The local variable area in the swap stack frame stores references to parameter objects E1 and E2. Although the reference values of E1 and E2 are the same as the worker and manager, they take up different memory space. When the E1 and E2 references are exchanged, the following figure is clear that it does not affect the worker and manager reference values at all.

Java Object parameter passing is an address (reference), but is still a value call. It is time to call an exact definition of the invocation and value of the reference.

value Invocation (call by value): during parameter passing, formal parameters and arguments occupy two completely different memory spaces. The content stored by the formal parameter is a copy of the contents of the argument store. In fact, the delivery of a Java object conforms to this definition, except that the contents of the parameters and arguments are not the values of the variables in the general sense, but the addresses of the variables. Well, think back: The address of the variable is not a value!

Reference invocation (call by reference): In the process of parameter passing, the parameters and arguments are exactly the same chunk of memory space, both equals. In fact, formal parameter names and argument names are just different symbols in programming, and in the process of running the program, the space stored in memory is the most important. Different variable names do not indicate that memory storage space is used differently.

In general, the root of the two calls is not whether to pass a value or an address (after all, the address is also a value), but that the parameters and arguments occupy the same piece of memory space. As a matter of fact, the pointer parameter passing is also a value call, do not believe try the following C code it!

C code
  1. #include <stdio.h>
  2. void swap (int *a1,int *b1) {
  3. int *t=a1;
  4. A1=B1;
  5. b1=t;
  6. }
  7. int main () {
  8. int x1=100;
  9. int x2=200;
  10. int *a=&x1;
  11. int *b=&x2;
  12. printf ("%d%d\n", *a,*b);
  13. Swap (A, b);
  14. printf ("%d%d\n", *a,*b);
  15. return 0;
  16. }

However, C + + is a reference call, and this is a variable declaration method called a reference: int A; int &ra=a; Where Ra is the alias of a, there is no difference between the two in memory, occupying the same memory space. The argument passed by reference (alias) conforms to the feature of the reference call. We can try it.

void swap (int &a1,int &b1), the running result.

Is the "Doubts" Java method parameter a reference call or a value call?

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.