Java method parameter passing, value passing and reference passing, and String passing

Source: Internet
Author: User

A = 5 System. out. println ("first output:" + System. out. println ("third output:" + swap (a = 10 System. out. println ("second output:" +}

Third output: 5

Check that, although the value of a is changed in the swap method, the value of variable a in the main method remains unchanged. This verifies the above, if the basic data type is passed, the parameter value is changed in the method, but the actual parameter value cannot be changed...

Here I also want to explain that the String type does not belong to the basic data type, but it is not a reference data type. It is a special reference data type. Example:

String a = "Hello! "System. out. println ("first output:" + System. out. println ("third output:" + B = "Hello, Word" System. out. println ("second output:" +}

Result: Hello!
Second output: Hello, Word
Third output: Hello!

Why can't it be changed? Because the passing of a string is actually a value transfer, it cannot be changed ..........

[] A = {1, 2, 3 System. out. print ("result output before method call:" (I = 0; I <. length; I ++ System. out. print (a [I] + "" System. out. print ("\ n" System. out. print ("output result after the method is called:" (I = 0; I <. length; I ++ System. out. print (a [I] + "" swap (B [0] = 2 System. out. print ("output result after changing the value of parameter B:" (I = 0; I <B. length; I ++ System. out. print (B [I] + "" System. out. print ("\ n "}

Result output after changing the value of parameter B: 2 2 3
Result output after the method is called: 2 2 3

Check that the passed parameter is the value of the method parameter when the reference type is changed. The actual value will also be affected ............ if you still don't understand it, you can talk to me privately !!!

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.