Summary of parameter passing problems in JAVA-Linux general technology-Linux programming and kernel information. The following is a detailed description. There are two kinds of parameter passing problems in JAVA: one is passing by value (if it is a basic type) and the other is passing by reference (if it is? Too many?
Start with two examples:
1)
Public class Test2 {
Public static void main (String [] args ){
StringBuffer a = new StringBuffer ("");
StringBuffer B = new StringBuffer ("B ");
Operate (a, B );
System. out. println (a + "," + B );
}
Static void operate (StringBuffer x, StringBuffer y ){
X. append (y );
Y = x;
}
}
Output: AB, B
2)
Public class Test2 {
Public static void add3 (Integer I ){
Int val = I. intValue ();
Val + = 3;
I = new Integer (val );
}
Public static void main (String args []) {
Integer I = new Integer (0 );
Add3 (I );
System. out. println (I. intValue ());
}
}
Output: 0
First, we should understand that all parameter passing in JAVA is passed by value. If it is a basic type, copy a basic type to the input method. If it is a reference, copy a reference variable to the input method. If you understand these two points, you can understand the problems related to the method operation object. It is better to draw a picture pointing to an object to fully understand it.
Question 1st: When the operate method is called, two copies x and y that reference a and B are passed in. Both x and y point to the object referenced by the original a and B. X. append (y) performs operations on the object to which it points (that is, the object to which a points. X = y, however, only two copy variables are assigned values, which does not affect the objects pointed to by original B. So the object that B points to is still B.
Question 2nd, I = new Integer (val) is just a reference copy pointing to another object, and the original I still points to the object new Integer (0.
If we grasp that all values in JAVA are transmitted and all the values are copied, we can solve similar problems.
There is only one way to pass parameters in Java: by value. Theoretically speaking, it is too troublesome. Let's look at some examples:
1). Basic Type
Public class {
Public static void main (String [] args ){
Int x = 1;
System. out. println (x); // 1
Test (x );
System. out. println (x); // or 1 ==> By value
}
Static void test (int ){
A = 2;
}
}
2). Reference Type
Public class B {
Public static void main (String [] args ){
Integer x = new Integer (1 );
System. out. println (x );
Test (x );
System. out. println (x );
}
Static void test (Integer ){
A = new Integer (2 );
}
}
The key to understanding this is to distinguish between objects and references. The x declared here is a reference, not an object (only Java designed it to look like an object ). This reference points to an object, which is the object generated later with the new keyword. Therefore, x points to an Integer object.
When the test method is called, the program passes x as a parameter to the test method. Here it is still the value transfer. A new reference (or y) will be generated during the test call process ). In this case, x and y point to the same object.
X and y point to the same object. Due to Java design, we can use operation references to operate on objects. Therefore, if we use y to modify the attributes of an object (for example, y. someField ++), we can see that the object pointed to by x is also modified.
On the other hand, if we let y point to another object, y = new Integer (2); then x and y point to different
Object. Y modifies the attributes of the object to which it points, which obviously does not affect the object to which x points.
Someone mentioned arrays. An array is also a reference type. Its parameter transmission method can be interpreted as follows:
Import java. util. Arrays;
Public class {
Public static void main (String [] args ){
Int [] aa = {3, 2, 1 };
System. out. println (Arrays. toString (aa); // [3, 2, 1]
Test (aa );
System. out. println (Arrays. toString (aa); // [3, 2, 1]
Test2 (aa );
System. out. println (Arrays. toString (aa); // [4, 2, 1]
}
Static void test (int [] ){
A = new int [] {1, 2, 3}; // points to the new object
}
Static void test2 (int [] ){
If (! = Null & a. length> 0)
A [0] ++; // modify the original object
}
}
Objects are passed references, and simple types are passed values. Do not be confused by some concepts on the internet !!! You can perform a test on your own.
String or other types are still referenced. If you use the concat method, the original value of the String object will be changed.
However, if you use the following method:
Public class Test {
Public static void test (String str ){
Str = "World ";
}
Public static void main (String [] args ){
String string = "Hello ";
Test (string );
System. out. println (string );
}
}
Running result: Hello
Str = "World" is equivalent to String str = new String ("World "). The result is not changed!
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