Java Basics-Learning notes (eight)--parameter passing of functions

Source: Internet
Author: User

1. Parameter passing of basic data type

1 classPassvalue2 {3     4      Public Static voidmain (String [] args)5     {6         intX=5;7 Change (x);8System.out.println ("The current value is" +x);9     }Ten      Public Static voidChangeintx) One     { AX=3; -     } - } the /* - F:\java_example>java Passvalue - The current value is 5 - */
View Code

Argument x and Parameter x, although the names are the same, do not have the same memory space, and the scope is different. The scope of the parameter x is limited to the change (), and its value varies from first to 5 in main () to X, then to the value of X from 5 to 3 through change (), to the end of the call to change (), and to the memory of X (). In this series of processes, the value of x in the argument x, which is main (), has not changed, so the final print is 5

2. Parameter passing of reference data type

classpassvalue{intx;  Public Static voidmain (String [] args) {Passvalue obj=NewPassvalue (); obj.x=5;        Change (obj); System.out.println ("The current value is" +obj.x); }     Public Static voidChange (Passvalue obj) {obj.x=3; }}/*F:\java_example>java passvaluethe Current value is 3*/
View Code

, the formal parameter, obj, is a new reference handle, but its scope is limited to the change function, but it has changed the value of x in the object before it is destroyed, so the print is 3.

Java Basics-Learning notes (eight)--parameter passing of functions

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.