How to pass Java parameters

Source: Internet
Author: User

Original: http://blog.sina.com.cn/s/blog_59ca2c2a0100qhjx.html, I made some changes and added an example to add a comparison

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


1, the basic type of the transfer method of the number of parameters

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 results of this code execution are as follows:
Call the Add method before num=30
After calling the Add method num=30

The results of the program execution also illustrate this point, no matter how you change the value of the 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 run, the program opens up a block of memory in the stack memory, with a value of 30 in it, and a memory model for example:


Run to the Add () method, the program in the stack memory also opened up a piece of memory address AD8600, the value of num is passed in, the memory of this block is placed in the value of 30. After running param = 100, the value in AD8600 becomes 100. Memory models such as the following:

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvahd3bjiwmdk=/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center ">

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

2. How to transfer object type parameters

1) First look at the following demo sample code:

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 execution 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.

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvahd3bjiwmdk=/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center ">

After the program has run string[] array = new string[] {"Huixin"}. The program opens up a reference address in the stack memory for the address number AD9500 memory space to hold array[0]. The value placed inside is an address in the heap memory. The value in the Demo sample 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, which is 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.

The memory that can be understood as a number AD9600 in the stack memory has a pointer to the memory address in heap memory numbered BE2500, as seen in:


In this way, stack memory AD9500 and AD9600 (that is, array[0] and Param values) point to the heap memory that is numbered BE2500.


After the Reset method changes the value of param to Hello, world!, the memory model, for example, is seen as:


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, and the value stored inside it 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, and the value of array[0] found from the stack memory is BE2500. Then go to heap memory to find the memory numbered BE2500, inside the value 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;

Explain 10,10 first: when you run D1.move2 (D1). D1 (reference 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 running d1.move1 (D1),D1 (reference address) is passed into move1, the stack memory opens up space, 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, not just the form of the value.

The first demo sample is passed with the base type as a parameter. The value 30 in the stack memory is passed to the Add method.

The second demo sample passes the value BE2500 in the stack memory to the Reset method when the object is passed as a parameter. When the object is passed as a number of parameters. The true value is placed in the heap memory. The value in the stack memory is passed. The stack memory is stored in 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 basic type. That is, after the value of a variable of type string is changed in the method, the value of the string variable outside the method is not affected. About this problem. The following two addresses can be tested:
http://freej.blog.51cto.com/235241/168676
Http://dryr.blog.163.com/blog/static/58211013200802393317600/
I think it's the two reasons mentioned in the two articles. One is the wrapper class in which string actually operates char[], which can be understood as String is char[]. Second, after assigning the string variable again, it does not actually change the value of the variable, but it again new a string object. Changes the value of the new object, so the value of the original string variable does not change.

How to pass Java parameters

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.