Java programming basics-parameter transfer for method calls, java programming Basics

Source: Internet
Author: User

Java programming basics-parameter transfer for method calls, java programming Basics

There are two arguments for passing parameters when calling a function in Java code: Passing values and passing references. If the basic type of Java (int, boolean, etc.) is used as a parameter, it is considered to be a value, and if it is an object type, it is considered to be a reference. Next we will analyze the scenario of passing object references.

See the following Java basics:
 1 package com.elon.integertest; 2  3 public class StartupIntegerTest { 4     public static void main(String[] args) { 5         Integer i = new Integer(1); 6         System.out.println("a: i=" + i); 7         change(i); 8         System.out.println("c: i=" + i); 9         10         System.exit(0);11     }12     13     private static void change(Integer i) {14         int value = i + 2;15         i = new Integer(value);16         System.out.println("b: i=" + i);17     }18 }
After running the program, the following information is output:

 

According to the output, the I variable value is modified in the change () method, but this modification does not bring out the method. After exiting the change () method, the I value is still 1. The parameter I is an integer object type, and the object reference is passed in. It is correct only when the data can be brought out after modification. What is the problem? We need to analyze what is passed in when the change () method is called in the main function, and what is modified in the change () method.

 

Parameters are passed when a method is called, essentially copying data. Only the object content is copied, and the object address is copied by reference. In the above method call process, the address of the I variable is passed to the change () method, that is, a 32-bit pointer (64-bit operating system, pointer is 64-bit); In change () the method defines a variable I to store this address (note that the variable I in the change method is not the same variable as the variable I in the main method, but they all point to a variable space with a value of 1 at the beginning ).

 

Since two I variables are not the same, in the change () method, assign a value to I by applying for a new space, just let the I variable in the change () method point to a new address, it has no effect on the I variable in the main method and will not change the value it points to in the address space.

 

Verification in C ++ code

The address of the variable cannot be seen in JAVA. Put the above Code logic in C ++ and debug it.

1. The address of the int type variable pointed to by the p pointer at the beginning is 0x06bd5398:

 

2. The address of the p variable itself is 0x0031efac, that is, the pointer to the pointer is 0x0031efac:

 

3. After calling change (), we can see that the value of p is 0x06bd5398:

 

4. The address of the p variable itself changes to 0x0031ef70, indicating that the change () function defines a new pointer variable, which points to the same int variable as the p pointer in main.

 

Based on the above analysis, re-allocate an int object in change () and use the p pointer to point to it. Changing the value of the newly assigned variable does not affect the value of the int variable allocated in main.

 

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.