Differences between java method parameters and C method parameters

Source: Internet
Author: User
Tags call by reference

Call by is a standard term in computer science. Method calls are divided into call by reference and call by value based on the passing of parameters ). There are many definitions of these two types of calls. The most common saying is that the value is passed, and the address is the reference call. This is not appropriate. These statements can easily remind us that the object parameter passing in Java is a reference call. In fact, the object parameter passing in Java is still a value call.
First, we use a piece of code to verify why Java's object parameter passing is a value call.

Public class Employee {
 
Public String name = null;

Public Employee (String n ){
This. name = n;
}
// Exchange two Employee objects
Public static void swap (Employee e1, Employee e2 ){
Employee temp = e1;
E1 = e2;
E2 = temp;
System. out. println (e1.name + "" + e2.name); // print the result: James Li
}
// Main Function
Public static void main (String [] args ){
Employee worker = new Employee ("James ");
Employee manager = new Employee ("Li Si ");
Swap (worker, manager );
System. out. println (worker. name + "" + manager. name); // The printed result is still: Zhang San, Li Si
}
}
The above results are disappointing. Although the content of the parameter e1 and e2 is exchanged, the actual parameter worker and manager are not interchangeable. The most important reason is that the parameter e1, e2 is the address copy of the real parameter worker and manager.

As we all know, in Java, the object variable name actually represents the address of the object in the heap (specialized term is called object reference ). When calling a Java method, the parameter passes the object reference. It is important that the memory address occupied by the form parameter and the real parameter is not the same. The content in the form parameter is only a copy of the Object Reference stored in the real parameter. Java multi-thread download of network resources

If you know something about the local variable area of the Java stack in JVM memory management (see Java virtual machine architecture), you can understand the above sentence very well. When the JVM runs the above program, the main method and the swap method will push two Memory Spaces called Stack frames successively in the Java stack. The main stack frame contains a piece of memory called the local variable area, which is used to store references of the real parameter objects worker and manager. The local variable area in the swap stack frame stores the reference of the form parameter to the image e1 and e2. Although the reference values of e1 and e2 are the same as those of worker and manager, they occupy different memory spaces. When e1 and e2 references are exchanged, the following figure clearly shows that the reference values of worker and manager are not affected at all.

Although the Java object parameter passes an address (reference), it is still a value call. It is time to call an accurate definition for the reference call and value.

Call by value: During parameter transfer, the form parameters and real parameters occupy two completely different memory spaces. The content stored by the parameter is a copy of the content stored by the real parameter. In fact, the passing of Java objects conforms to this definition, but the content stored by the form parameters and real parameters is not the variable value in the general sense, but the address of the variable. Come back and think about it: isn't the variable address a value!

Call by reference: During parameter transfer, the parameters and real parameters are in the same memory space. In fact, the form parameter name and real parameter name are only different symbols in programming. During the program running, the space stored in the memory is the most important. Different variable names do not indicate that the occupied memory and storage space are different.
In general, the two types of calls do not involve passing a value or an address (after all, an address is also a value), but whether the form parameter and real parameter occupy the same memory space. In fact, the passing of pointer parameters in C/C ++ is also a value call. Do not believe it. Try the following C code!

# Include <stdio. h>
Void swap (int * a1, int * b1) {EMS Guide
Int * t = a1;
A1 = b1;
B1 = t;
}
Int main (){
Int x 1 = 100;
Int x 2 = 200;
Int * a = & x1;
Int * B = & x2;
Printf ("% d \ n", * a, * B );
Swap (a, B );
Printf ("% d \ n", * a, * B );
Return 0;
}

Author: "The blog of daemon"

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.