About parameter passing in Java

Source: Internet
Author: User

The problem was introduced because I saw a problem with a netizen in the group in the afternoon:

Given the following Java code, after compilation and running, the output result is ().
Public class test {
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;
}
}
A. a, B. AB, B c. A, AB d. AB, AB

A friend can guess the answer. I also want to tell you the answer: B. Some may be surprised, but some may be calm. Because stringbuffer is a class, for Class usage, create a class instance in the heap through new, and use a reference to perform operations on this object. When a reference is passed to a function as a parameter, C ++ uses the following address. In Java, the concept of address is not used. Everything is a value transfer. How can this problem be controlled.

First, I did a test:

Public class text {

Public static void main (string [] ARGs ){

String S = "B ";

F (s );

System. Out. println (s );

}

Static void F (string s ){

S = "C ";

}

}/* Output

B

*///~

The result shows that the effect of the parameter does not apply to the real parameter. Let's continue to look at the following code:

Public class text {

String S = "B ";

Public static void main (string [] ARGs ){

Text T = new text ();

F (t. s );

System. Out. println (T. s );

}

Static void F (string s ){

S = "C ";

}

}/* Output

B

*///~

The output result here is still B. Continue:

Public class text {

String S = "B ";

Public static void main (string [] ARGs ){

Txet T = new text ();

F (t );

System. Out. println (T. s );

}

Static void F (Text t ){

T. S = "C ";

}

}/* Output

C

*///~

From the results, we can see that the effect of the form parameter applies to the real parameter, and the reference here has an effect.

String is the system class, while text is the custom class. To prevent the impact of the custom class and system class, another list of System Classes is used for verification:

Public class text {

Public static void main (string [] ARGs ){

List <integer> List = new arraylist <integer> ();
List. Add (33 );
L (list );
System. Out. println (list );

}

Static void L (list <integer> List ){
List. Add (55 );
}

 

}/* Output

[33, 55]

*///~

From the output results, we can see that the reference effect of the class can be realized.

In response to the above results, I think there will be some surprises, because in Java BASIC language learning, we once () the operation has been discussed many times. The result shows that the string class is a reference type and a piece of code is provided for you to learn:

Public class simpletypetext {

Public static void main (string [] ARGs ){
String S1 = "";
String S2 = "";
String S3 = new string ("B ");
String S4 = new string ("B ");
System. Out. println ("S1 = S2? "+ (S1 = S2 ));
System. Out. println ("s1.equals (S2 )? "+ S1.equals (S2 ));

System. Out. println ("S2 = S3? "+ (S2 = S3 ));
System. Out. println ("s2.equals (S3 )? "+ S2.equals (S3 ));

System. Out. println ("S3 = S4? "+ (S3 = S4 ));
System. Out. println ("s3.equals (S4 )? "+ S3.equals (S4 ));

(S4, S1 );
}
Static void F (string s ){
S = "";
}
Static void g (string S, string S1 ){
S = "";
System. Out. println ("via regular string:" + S = S1 );
S = new string (S1 );
System. Out. println ("using the new method:" + S = S1 );
}
}/* Output:

S1 = S2? True
S1.equals (S2 )? True
S2 = S3? False
S3.equals (S3 )? True
S3 = S4? False
S3.equals (S4 )? True
Use a regular string: True
Use the new method: false

*///~

 

Summary:

1. Thinking in Java has the following description for string objects:

The string object is immutable. None of the methods seem to modify the string value are actually creating a brand new String object.

Parameters for string objects are described as follows:
Every time a String object is used as a method parameter, a reference will be copied, and the object referred to by this reference has never been moved in a single physical location.

2. in the declaration of a string, when a constant string is initialized, the system will search for the existing constant string. If this string constant exists, it will not generate a new String constant, but the new method is not like this, that is, it does not search, directly open up a new space, store this string.

 

Declaration: The above code is for reference and may be helpful to the basics.

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.