search
HomeJavaJavagetting StartedHow to pass parameters in java parameters

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools