Passing parameters in Java (passing by value and referencing)

Source: Internet
Author: User

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 an object ).

 

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 !!!

Is there any exception in the following program at 1? If not, what is the output? Will it run at 2? If yes, what is the output? Why is there such a result?

 

Import java. util. arraylist;
Import java. util. List;
  
Public class testclass {

Public static void main (string ARGs []) {
List list = new arraylist ();
Test2 (list );
System. Out. println (list. Size (); // 1
Test3 (list );
System. Out. println (list. Size (); // 2
}
  
Public static void Test2 (list ){
List = NULL;
}
  
Public static void test3 (list ){
List. Add ("aaaa");
}
}

Plumechen:

No error. The result is 0, 1.

Because Test2 (list) is passed as a list reference, I understand it as a copy of the pointer, list = NULL; just set the input value to null, the pointer and content of the original list are not changed. Test3 (list) is passed in the same way, but the list is executed. add () because the copy of the passed pointer value also points to the address of the original list, the content of the original list is changed, and the size is changed to 1.

Related Article

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.