Home  >  Article  >  Java  >  How to pass parameters in java parameters

How to pass parameters in java parameters

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼Original
2019-12-09 17:01:515371browse

How to pass parameters in java parameters

There are two ways to pass parameters to subroutines in computer languages:

Pass by value (call-by-value): This method copies a parameter value into a formal parameter of the subroutine. In this way, changes to the parameters of the subroutine do not affect the parameters of the call to it.

Call-by-reference: In this method, a reference to the parameter (rather than the parameter value) is passed to the subroutine parameter. In the subroutine, the reference is used to access the call. The actual parameters specified. In this way, changes to the subroutine parameters will affect the parameters of the calling subroutine.

In Java, when you pass a simple type to a method, it is passed by value. Therefore, changes to the parameters of a subroutine that receives parameters will not affect outside the method. Please look at the following example:

package toSzifucuan;
 
class Teshks{
void meth(int i, int j) {
i*=2;
j/=2;
}
}
 
public class CallByValue {
 
public static void main(String[] args) {
Teshks oob=new Teshks();
int a=15, b=20;
System.out.println("a和b初始值:"+a+" "+b);
 
oob.meth(a, b);
System.out.println("a和b在计算后的值:"+a+" "+b);
 
}
 
}

The result given by the program is:

Initial values ​​of a and b: 15, 20

The calculated values ​​of a and b :15,20

It can be seen that the operations that occur inside meth() do not affect the values ​​of a and b in the call.

This situation changes when you pass an object to your method, because the object is passed by reference. Remember that when you create a variable of class type, you are just creating a reference to the class. Therefore, when you pass this reference to a method, the parameter that accepts it will point to the same object that the parameter points to. This is strong evidence that the object is passed to the method via a reference call. Changes to the objects in this object do affect the object as a parameter. For example:

package toSzifucuan;
 
class Teshks{
int a, b;
public Teshks(int i, int j) {
a=i;
b=j;
}
void meth(Teshks oo) {
oo.a*=2;
oo.b/=2;
}
}
 
public class CallByValue {
 
public static void main(String[] args) {
Teshks oob=new Teshks(15,20);
System.out.println("a和b初始值:"+oob.a+" "+oob.b);
 
oob.meth(oob);
System.out.println("a和b在计算后的值:"+oob.a+" "+oob.b);
 
}
 
}

The result printed by this program is:

Initial values ​​of a and b: 15, 20

The calculated values ​​of a and b: 30, 10

In this example, the operations in meth() affect the object as a parameter.

When an object reference is passed to a method, the reference itself is passed using call-by-value. However, because the value being passed points to an object, the copy of the value still points to the same object that its corresponding argument points to.

Note: Use pass by value when a simple type is passed to a method. Objects are passed by reference.

PHP Chinese website has a large number of free JAVA introductory tutorials, everyone is welcome to learn!

The above is the detailed content of How to pass parameters in java parameters. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn