How Java parameters are passed

Source: Internet
Author: User

Original: http://blog.sina.com.cn/s/blog_59ca2c2a0100qhjx.html, I made some changes and added an example to increase the contrast

This article discusses parameter passing in Java by means of a memory model.


1, the basic type parameter transfer way

The memory model here involves two types of memory: Stack memory (stack) and heap memory (heap). When the base type is passed as a parameter, a copy of the value is passed. No matter how you change the copy, the original value will not change. Look at the code below and then combine the memory model to illustrate the problem:

public class Parametertransfer {public static void main (string[] args) {  int num =;  SYSTEM.OUT.PRINTLN ("Call the Add method before num=" + num);  Add (num);  SYSTEM.OUT.PRINTLN ("Call the Add method after num=" + num); } public  static void add (int param) {  param = 100;}}
The result of this code run is as follows:
Call the Add method before num=30
After calling the Add method num=30

The results of the program run also illustrate this point, no matter how you change the value of the parameter Param in the Add () method, the original value num will not change.
The bottom is analyzed by the memory model.
When an int num = 30 is executed, the program opens a block of memory in the stack memory with a value of 30 and a memory model such as:--AD8500


Execute to the Add () method, the program in the stack memory and open up a piece of memory address AD8600, the value of num is passed in, the value of this memory is 30, the execution of param = 100, after the value in AD8600 becomes 100. Memory models such as:


The value for storing Param in address AD8600 has nothing to do with the memory that holds NUM, no matter how you change the value of Param, the actual change is the value in memory with the address AD8600, and the value in AD8500 does not change, so the value of NUM does not change.

2. How to pass the object type parameter

1) Look at the sample code below:

public class Parametertransfer {public static void main (string[] args) {  string[] array = new string[] {"Huixin"};
   system.out.println ("The value of the No. 0 element in the array before the Reset method is called is:" + array[0]);  Reset (array);  System.out.println ("The value of the No. 0 element in the array after calling the Reset method is:" + array[0]);}  public static void Reset (string[] param) {  param[0] = "Hello, world!";}}
The results of the operation are as follows:
The value of the No. 0 element in the array before calling the Reset method is: huixin
After calling the Reset method, the value of the No. 0 element in the array is: Hello, world!
When an object is passed as a parameter, it is passed a reference to the object, which is the address of the object. Use the memory model diagram below to illustrate.


When the program executes string[] array = new string[] {"Huixin"}, the program in the stack memory opens a piece of address numbered AD9500 memory space, for storing array[0] The reference address, the value is placed in the heap memory of an address, The value in the example is BE2500, which can be understood as having a pointer pointing to the address in heap memory numbered BE2500. The value of array[0] is stored in this address in heap memory numbered BE2500: Huixin.
When the program enters the reset method, the value of the array, the object's reference BE2500, is passed in. At this time, the program in the stack memory and opened up a piece of memory space numbered AD9600, the value is passed over the value, that is, AD9600. It can be understood that there is a pointer in memory numbered AD9600 in the stack memory and also points to the memory address in heap memory numbered BE2500:


In this way, stack memory AD9500 and AD9600 (that is, array[0] and Param values) point to the heap memory that is numbered BE2500.
In the Reset method, modify the value of param to Hello after world!, as shown in the memory model:


Changing the value of the object param is actually changing the value in the heap memory that param this stack of memory points to. Param the address of this object in the stack memory is AD9600, the value stored inside is BE2500, so the value in the heap memory BE2500 becomes hello,world!. When the program is put back to the main method, the value in heap memory BE2500 is still the value of array[0] in the Hello,world!,main method, the value of array[0] found from the stack memory is BE2500, and then the memory of the heap is looked up in memory numbered BE2500. The value inside is hello,world!. So the value printed in the main method becomes the hello,world!

2) Let's look at another example.

Class Dog{int X,y;int z=1;dog (int x,int y) {this.x=x;this.y=y;} void Move1 (Dog d) {d.x=d.x+10;d.y=d.y+10;} void Move2 (dog d) {dog d2=new dog (+);d =d2;}} public class Usedog {public static void main (string[] args) {//TODO auto-generated method Stubdog d1=new Dog (10,10);d 1.mo Ve2 (D1); System.out.print (d1.x+ "," +d1.y+ ":");d 1.move1 (D1); System.out.println (d1.x+ "," +d1.y+ ";");}}
The output result is: 10,10:20,20;

First explain 10,10: when the D1.move2 (D1) is executed, the D1 (referring address) is passed into the move2, and the stack memory opens up space to deposit. After the new object is D2 and the D2 (reference address) is assigned to D, D points to the object D2. Therefore, the heap memory value pointed to by D1 is not changed.

Explain 20,20: When executing d1.move1 (D1), theD1 (reference address) is passed into the move1, and the stack memory space is deposited, that is, D points to the heap memory value pointed to by D1. When you change the value of the object that D points to in move1, the value of the object that D1 points to also changes. Because D and D1 point to the same object.


3. Summary

whether the base type is passed as a parameter, or if the object is passed as a parameter, the value is actually passed, only the form of the value is not used. The first example passes the value 30 in the stack memory to the Add method when the base type is passed as a parameter. The second example passes the value BE2500 in the stack memory to the Reset method when the object is passed as an argument. When the object is passed as a parameter, the real value is placed in the heap memory, the value in the stack memory is passed, and the stack memory is the address of the heap memory, so the address of the heap memory is passed. This is the difference between them.

To add, in Java, string is a reference type, but when passed as a parameter, it is an attribute of the primitive type, that is, when the value of a variable of type string is changed in a method, the value of the string variable outside the method is not affected. For this question, you can refer to the following two addresses:
http://freej.blog.51cto.com/235241/168676
Http://dryr.blog.163.com/blog/static/58211013200802393317600/
I think it is the two reasons mentioned in these two articles, one is that string actually operates char[], which can be understood as a wrapper class of string char[]. Second, after the string variable is re-assigned, actually did not change the value of the variable, but instead of re-new a string object, changed the value of the new object, so the original string variable value has not changed.

How Java parameters are passed

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.