Passing Java values, passing references, and passing arrays !!

Source: Internet
Author: User

Many programming languages have two ways to pass parameters to methods-pass by value and by reference.

 

Different from other languages, Java does not allow programmers to choose whether to PASS Parameters by value or by reference. The basic type is byte -- short -- int -- long -- float -- double -- Boolean -- char) variables are always passed by value. For an object, instead of passing the object itself to a method, it passes the reference of the object or the first address of the object to the method. The reference itself is passed by value ----------- that is to say, the referenced copy is passed to the method (the copy indicates that there are two references to the object at this time, method can be used to directly operate the object (the object can be changed only when the object is operated, but the source object is not changed when the object is referenced ).

 

Let's talk about Arrays: if an element of a single basic type array is passed to a method and modified in the method, when the method is called, this element does not store the modified value because it is passed by value. If it is an array reference, subsequent modifications to the array elements can be reflected in the original array (because the array itself is an object, int [] A = new int [2];, here, the int type is an array element, and the modification of the array element is an operation object ).

 

When an element of a non-basic type array is modified in a method, the modified value is stored in the element at the end of the called method, because this element is passed by reference, changes to the object will be reflected in the array element of the source array.

 

The following is a small program:

  1. Public class test {
  2. String STR = new string ("good ");
  3. Char [] CH = {'A', 'B', 'C '};
  4. Int I = 10;
  5. Public void change (string STR, char [] CH, int I ){
  6. STR = "test OK ";
  7. Ch [0] = 'G ';
  8. I ++;
  9. }
  10. Public static void main (string [] ARGs ){
  11. Test TT = new test ();
  12. TT. Change (TT. STR, TT. ch, TT. I );
  13. System. Out. println (TT. I );
  14. System. Out. Print (TT. Str + "and ");
  15. System. Out. println (TT. ch );
  16. }
  17. }

STR is a string type reference, I is a basic type variable, CH is an array name, and also an array Object Reference

In the Chang () method, STR = "test OK" is a new object that places the first address on the reference variable STR;

Ch [0] = 'G'; because the array is referenced, CH [0] = 'G'; is used to operate on array elements, can modify the content of the source array;

I is an integer value. I just copies the value to the method. The method changes without changing the source I.

The result is:

10

Good and GBC

 

Now let's change the code:

  1. Public class test {
  2. String STR = new string ("good ");
  3. Char [] CH = {'A', 'B', 'C '};
  4. Int I = 10;
  5. Public void change (string STR, char CH, int I ){
  6. STR = "test OK ";
  7. Ch = 'G ';
  8. This. I = I + 1;
  9. }
  10. Public static void main (string [] ARGs ){
  11. Test TT = new test ();
  12. TT. Change (TT. STR, TT. ch [0], TT. I );
  13. System. Out. println (TT. I );
  14. System. Out. Print (TT. Str + "and ");
  15. System. Out. println (TT. ch );
  16. }
  17. }

 

Observe the changes of real parameters and input parameters carefully?

The input parameter char [] CH in the change () method becomes -------------- char ch;

This is a single array element of the char value. According to the above parsing, CH = '9'; does not affect the source array element.

This. I = I + 1; here, the I on the left of the equal sign is attribute I, and the I on the right of the equal sign is a local variable (I in the input parameter );

At this time, I + 1 is assigned to the property I, which naturally changes the value of the property I. in Row 17, TT. I is the call property I. The result of this operation is:

 

11

Good and ABC

 

Do you understand it now?

Let's look at the following small program.

  1. Public class test {
  2. Public void change (stringbuffer X, stringbuffer y ){
  3. X. append (y );
  4. Y = X;
  5. }
  6. Public static void main (string [] ARGs ){
  7. Stringbuffer Buffa = new stringbuffer ("");
  8. Stringbuffer buffb = new stringbuffer ("B ");
  9. New Test (). Change (Buffa, buffb );
  10. System. Out. println (Buffa + "," + buffb );
  11. }
  12. }

This operation passes the reference values of two objects,

In method change (), X. append (Y), where X is referenced to call the API method append () to modifyNewStringbuffer (".

Y = x; it is a modified object that assigns the first address to the reference variable Y. In this case, the reference is operated, whereas y isNewStringbuffer ("B"); reference variable, so the output result is:

AB, B

 

The following is a slightly difficult small program. First, write your own results in the painting process, and then perform operations on the computer. If your results are the same as those on the computer, it is not difficult to encounter this type of question again. If it is different, I will go back and explain it carefully to find the reason.

  1. Public class test {
  2. Private string nn = new string ("1 ");
  3. Private string [] Mm = {"2", "5 "};
  4. Void test (string NN, string [] mm ){
  5. Nn = new string ("3 ");
  6. This. nn = "9 ";
  7. Mm [0] = "4 ";
  8. System. Out. println ("in test (), Mm [0]:" + mm [0]);
  9. Mm = new string [] {"8", "7 "};
  10. System. Out. println ("in test (), NN:" + nn );
  11. System. Out. println ("This. nn:" + this. nn );
  12. System. Out. println ("Mm [0]:" + mm [0]);
  13. }
  14. Public static void main (string [] ARGs ){
  15. Test S = new test ();
  16. S. Test (S. nn, S. mm );
  17. System. Out. println (S. Nn + "" + S. Mm [0]);
  18. }
  19. }

 

 

 

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.