Some opinions on passing a value to a reference using java method parameters

Source: Internet
Author: User

Different from C ++, java's method parameters are passed values without reference. Many people think that this is a reference, and this incorrect opinion is common. Let's analyze it here. There are basic types of method parameters, such as int. The other type is OBJECT and OBJECT. For details, the method parameter is an object. Different from C ++, the JAVA method parameter is used to transmit a copy of the object reference, that is, the address value, it is the same object as the original reference. Therefore, all JAVA method parameters are passed in values.
The object value passed by the program is changed.
Public class Test
{
Public Test ()
{
Int [] count = {5 };
Change (count );
System. out. println (count [0]);
}
Public void change (int [] counts)
{
Counts [0] = 6;
System. out. println (counts [0]);
}
Public static void main (String [] args)
{
Test t = new Test ();
}
}
In Program 1, all output results are 6, change (count); copy a reference of the object and send it to change (). Both the copy and the original reference point to the same object. So the value of the reference copy is changed, and the object value is also changed.
Program 2, the passed object value does not change
Class Foo {
Int I = 0;
}
Public class {
Public static void main (String args []) {
Foo f = new Foo ();
Add (f );
System. out. println (f. I );
}
Public static void add (Foo f ){
F = new Foo ();
F. I ++;
}
}
The output result is 0.
In program 2, it also transmits a reference copy, but in the add () method, f = new Foo (); NEW has a new object, therefore, the referenced copy point to a new OBJECT. The original value remains unchanged.
Program 3 String does not change, array changes
Public class Example {
String str = new String ("good ");
Char [] ch = {'A', 'B', 'C '};
Public static void main (String args []) {
Example ex = new Example ();
Ex. change (ex. str, ex. ch );
System. out. print (ex. str + "and ");
Sytem. out. print (ex. ch );
}
Public void change (String str, char ch []) {
Str = "test OK ";
Ch [0] = 'g ';
}
}
Program 3 outputs good and gbc.
String is special because java treats string as the basic type when passing a string. So the value remains unchanged.

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.