java method parameters

To better understand the method parameters of the word, we must first understand what the method.

method:

We can first look at the definition of the method:

  •    访问修饰符] [static] [是否有返回值类型] 方法名(参数列表){
    
  •    [方法执行体]
    
  •    [return返回值]
    
  •    }
    

For example, a user-friendly, Person class to represent an object or a class object here means "people"
who have eyes, mouth these are the attributes of the people
who can do what? A: What do do () method that people can do
is action method is the object of this action will produce what kind of effect

A simple method declaration:
public void getName () {

}

The method parameters in what we call parameters are parameters.
Java programming language always uses call by value. That is, the method is to obtain a copy of all the parameter values, in particular, the method can not modify its contents transferred to any parameter variables.

I can write a small example:
Here Insert Picture Description
we declare a, b two variables in the main function and initialization, call swapOne method, the final output values of a and b.
Here Insert Picture Description
From the results, we found that the value of a and b does not change. This is the use of Java references by value.
So what is this principle of it?
Here Insert Picture Description
As can be seen from this figure, a and b in the stack above and b is a variable declared in our main function, following a and b are parameters of our method, the method is a copy of the parameter values. Two points a and b in the stack while a = 10 and b = 20.
When we call the method, a and b values do exchange method is copied exchanging a and b values. A and b values on our main function is not declared in the exchange. So we output a and b or the original value.

Creating an Employee class

Here Insert Picture Description
Creating an Employee class and declare constructors and methods getname setname and
Here Insert Picture Description
write the name of the two men exchanging method of the Employee class
Here Insert Picture Description
final statement of the two variables and apply the Employee class space in the main function, perform switching function and output person1 and person2
Here Insert Picture Description
Finally we get found the result is still not exchanged success.
The same reason earlier.
And what we change our exchange method
Here Insert Picture Description
then declare person3 and person4 variable and space applications, and perform switching method of output value person3 and person4
Here Insert Picture Description
Here Insert Picture Description
found that the results of this exchange was successful.
Here Insert Picture Description
Because we declare a string c and () method to get the value of the stack in a wangwu directed by getName
with setName () and getName () method directly change the value in the stack.
Thus person3 and person4 pointed to the value on the ensuing changes.

This procedure shows: Java programming language is not a reference to an object using calls, in fact, it is an object reference is passed by value.

The following summarizes what java method parameters are:
1. The method does not substantially modify a parameter of type
2. A method of an object can change state parameter.
3. The method does not make a reference to a new object parameter object.

Guess you like

Origin blog.csdn.net/fight252/article/details/89216604