Is the Java method parameter a reference call or a value call? _java

Source: Internet
Author: User
Tags call by reference

A method call (called by) is a standard computer science term. A method invocation, which is passed by a parameter, is divided into a value invocation (call by reference) and a reference invocation (call by value). There are a lot of definitions of these two kinds of calls, the most common saying is to pass the value of the value of the call, the address is to pass the reference call. This is actually very inappropriate, this kind of argument is very easy to remind us that Java object parameter passing is a reference call, in fact, Java object parameter pass is still a value call.

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

public class Employee {public 

  String name=null; 

  Public Employee (String N) {; 
  Exchange two employee objects for public 
  static void swap (employee E1,employee E2) { 
    employee temp=e1; 
        System.out.println ( ""; Print Result: Li Shizhang 
  //main function public 
  static void Main (string[] args) { 
    employee Worker=new employee ("John"); C15/>employee manager=new Employee ("Dick"); 
    Swap (Worker,manager); 
    System.out.println ( ""; Print result is still: Dick 

The above result is very disappointing, although the formal parameter object e1,e2 the content exchange, but the argument object Worker,manager does not exchange the 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 all 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 Java method invocation, the parameter passes a reference to the object. Importantly, formal parameters and arguments are not the same memory addresses, and the content in the formal parameters is only a copy of the object reference stored in the argument.

If you know something about the local variable area of the Java stack in JVM memory management (see the Java Virtual Machine architecture), you can understand the above sentence very well. When the JVM runs the above program, running the main method and the swap method pushes two memory spaces, called stack frames, in the Java stack. A memory in the main stack frame called a local variable area is used to store references to the arguments object worker and manager. The local variable area in the swap stack frame stores references to the formal parameter objects E1 and E2. Although E1 and E2 reference values are the same as worker and manager, they occupy different memory space. When the E1 and E2 references are exchanged, the following figure clearly shows no effect on the worker and manager reference values at all.

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

Value invocation (call by value): The formal parameters and arguments occupy two completely different memory spaces during parameter passing. A formal parameter stores a copy of the content that the argument stores. In fact, the passing of a Java object conforms to this definition, except that the contents of a formal parameter and an argument are not a variable value in the normal sense, but a variable's address. Well, think back: The address of the variable is not a value!

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

In general, the two calls do not simply pass a value or an address (after all, the address is a value), but rather whether the formal parameters and arguments occupy the same memory space. In fact, C + + pointer parameter transfer is also a value call, do not believe to try the following C code Bar!

#include <stdio.h> 
void swap (int *a1,int *b1) { 
  int *t=a1; 
int main () { 
  int x1=100; 
  int x2=200; 
    int *a=&x1; 
  int *b=&x2; 
  printf ("%d%d\n", *a,*b); 
  Swap (a,b); 
  printf ("%d%d\n", *a,*b); 
  return 0; 

But C + + is a reference call, and this is a variable declaration method called a reference: int A; int &ra=a; Where Ra is an alias for a, the two are not different in memory and occupy the same memory space. The parameter passing by reference (alias) is consistent with the characteristic of reference invocation. You can try void swap (int &a1,int &b1), the results of the operation.

In this article, you should know whether the Java method parameter is 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: 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.