JAVA basics -- passing parameters through methods

Source: Internet
Author: User
Tags call by reference

 

JAVA basics -- passing parameters through methods

Why take this as a topic?

In the beginner's stage, many children's shoes are confused about how to pass parameters.

I. First, let's talk about several terms of parameter passing:

Call by vale: indicates that the method receives the value passed by the caller.

Call by reference: indicates that the method receives the variable address passed by the caller.

A method can modify the value of a variable that passes the reference, but cannot modify the value of a variable that calls the passed value;

** The JAVA language always uses value calls.

That is to say, the JAVA method obtains a copy of all parameter values, and the method cannot modify the content of any parameter variables passed to it.

For example:

Int age = 100;

Person. setAge (age );

After the preceding method is called, the value of age is still 100;

Let's look at another example. If a method modifies a change value to three times the original value:

Public void addValue (int x ){

X = 3 * x;

}

Then call this method:

Int percent = 10;

AddValue (percnet );

No matter how you call the percent value or 10, the following is the execution process:

1. x is initialized as a copy of the percent value (that is, 10 );

2. After x is multiplied by 3, the value changes to 30. But percent is still 10;

3. After the method is completed, the parameter variable x is no longer used.

I:

Ii. java method parameter passing type:

 

Basic Data Type: (numeric, Boolean)

Object Reference Type:

As mentioned in the preceding example, the value of the basic data type parameter cannot be modified.

Can the parameter passing of the object reference type be modified?

Let's take a look at the example:

There are the following methods:

Public static void tripleSalary (Employee x)

 

{

X. raiseSalary (200 );

}

When the following code is called:

Harry = new Employee ();

TripleSalary (harry );

The specific execution process is as follows:

1). x is initialized as a copy of the harry object. Here is an object reference;

2). The raiseSalary method is applied to this object reference. That is, the salary of the Employee object referenced by x and harry is increased by 200%.

3). After the method call is completed, the parameter variable x is no longer used, but the object variable harry continues to reference the salary to three times the Employee object.

The process is as follows:

Through the above example, we can see that the object parameter passes the copy of the object reference, and the object reference and its copy references the same object at the same time.

 

Many programming languages provide two transfer methods: value transfer and reference transfer (C ++ and Pascal ).

Some Programmers think that the java language also uses reference calls for object parameter passing. In fact, it is an understanding error. This error is universal. The following is a counterexample to illustrate this problem:

First, write a method to exchange two Employee objects:

Public staitc void swap (Employee x, Emplyee y)

{

Employee temp = x;

X = y;

Y = temp;

}

If the JAVA programming language uses a reference call to an object, this method should be able to exchange data:

Employee a = new Employee ("Alice ",...);

Employee B = new Employee ("Bob ",...);

Swap (a, B );

However, the method does not change the object references stored in variables a and B. The parameters x and y of the swap method are initialized as copies of two object references. This method exchanges the two copies.

At the end of the method, the parameter variables x and y are discarded. The original variables a and B still reference the objects referenced before calling this method, for example:

This process shows that the java language does not use reference calls for objects. In fact, object references are passed through values.

 

Finally, we will summarize the usage of method parameters in JAVA:

* A method cannot modify a parameter of the basic data type;

* A method can change the state (attribute) of an object parameter );

* A method cannot allow object parameters to reference a new object;

This article is from the "when you start watching the cloud" blog

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.