Core Java (9) Java method parameters

Source: Internet
Author: User

In variousProgramThere are two methods in the design language: Value call and Reference call.

Java programming language is always usedValue call, that is, the method obtains a copy of all parameter values..

There are two types of method parameters:

    1. If the parameter is of the basic data type, a value of the basic data type is passed;
    2. If the parameter is an object,Copy object references. Note that it is not an object reference in C ++.. The referenced copy is the same as other copies, and an object is referenced at the same time.

A method cannot modify a parameter of the basic data type, but can modify the state of an object.

Be sure to note that in Java, the object is a copy of the reference, not a reference call. The inverse example is as follows:

Test procedure:

Package COM. xujin; public class test {public static void main (string [] ARGs) {employee [] Staff = new employee [2]; staff [0] = new employee ("Bob ", 1000); staff [1] = new employee ("Jim", 2000); // doesn' t workswap (staff [0], staff [1]); system. out. println (staff [0]. getname (); // bobsystem. out. println (staff [1]. getname (); // jimstaff [0]. raisesalary (0.5); // raise the salary by 50%. out. println (staff [0]. getsalary (); // 15001_system.out.println (staff [0]. GETID (); // 1system. out. println (staff [1]. GETID (); // 2} public static void swap (employee X, employee y) {employee temp = x; X = y; y = temp ;}} class employee {public employee (string name, double salary) {This. name = Name; this. salary = salary; id = nextid; nextid ++;} Public String getname () {return name;} public double getsalary () {return salary;} public int GETID () {return ID;} public void setname (string name) {This. name = Name;} public void setsalary (double salary) {This. salary = salary;} public void raisesalary (double percent) {This. salary * = (1 + percent);} private string name; private double salary; private int ID; Private Static int nextid = 1 ;}

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.