1 Overview
1. What is parameter passing?
The process of passing data to formal parameters when calling a method is called parameter passing. There are two modes of transfer in programming languages: transfer by value and transfer by reference. It must be emphasized that the two delivery methods mentioned here are not limited to the delivery methods used in java, but are the delivery methods that appear in many programming languages including java.
2. Variable typeIn java, we call variables pointing to basic type data primitive variables, and variables pointing to objects called reference variables.
2. Value passing
1. What is value passing?
Pass a copy of the variable into the method. Operations inside and outside the method are isolated. Operations on variables within the method will not be reflected in variables outside the method.
2. Original variable
public void change(int b) {
b = 7;
}
@Testpublic void testBasic() {int a = 9;
change(a);
System.out.println(a);
}
Actual output: 9When passing parameters, according to the rules of value passing, variable b receives a copy of a, which also points to the literal value "9":
##Next, inside the method, assign a value of 7 to b, so that b points to 7. Since a and b are two independent variables, there is no reference and reference relationship between them, and a still points to 9. :
##3.String
public void change(String str01) {
str01 = "baidu";
}
@Testpublic void testString() {
String str = new String("www.baidu.com");
change(str);
System.out.println(str);
}
Actual output: www.baidu.com
When passing parameters, str passes a copy of itself to str01, so that str01 also points to the heap storing "www.baidu.com" Object:
Assign a value to str01 inside the method, so that str01 points to "baidu" in the string constant pool in the method area ", str still points to "www.baidu.com" in the heap, str and str01 point to different objects, and do not affect each other:
One thing to note here: Java designs String as an immutable object, that is, once the literal value contained in the String object changes, Java will create a new object and point the variable to the new object.
4.StringBuilder
public void change(StringBuilder builder01) {
builder01.append(" World!");
}
@Testpublic void testStringBuilder() {
StringBuilder builder = new StringBuilder("Hello");
change(builder);
System.out.println(builder);
}
Actual output: Hello World!
After the parameter transfer is completed, the builder01 variable obtains a copy of the builder variable. The copy and the original variable point to the same object in the heap:
Inside the method, the variable builder does not point to a new object, but still points to the same object as builder, so when builder accesses the same object in the heap, the data changes:
5. Custom type
public class MyInner {public int a;
}public class Test{public void change(MyInner in01) {
in01.a = 1;
}
@Testpublic void testDemain() {
MyInner in = new MyInner();
in.a = 9;
change(in);
System.out.println(in.a);
}
}
Actual output: 1
The execution process is the same as that of StringBuilder, so I won’t go into details here. Let’s make some changes to the above code, as follows:
public class MyInner {public int a; }public class Test{public void change(MyInner in01) { in01=new MyInner();//使in01指向一个新的对象in01.a = 1; } @Testpublic void testDemain() { MyInner in = new MyInner(); in.a = 9; change(in); System.out.println(in.a); } }
实际输出:9
参数传递完成时,in01与in指向同一个对象,in01对对象的操作等同于in对对象的操作,接着在方法内部执行"in01=new MyInner();",这样in01就指向了一个新的对象,in01所有的操作都与in无关了:
综合以上的运行结果与分析,可知java参数传递方式符合值传递。
三 引用传递
1.什么是引用传递?
将变量自身的内存地址传入方法中,方法中的变量指向方法外的变量,在方法中对变量的操作就是对方法外变量的操作。
2.自定义类型
public class MyInner {public int a; }public class Test{public void change(MyInner in01) { in01=new MyInner();//使in01指向一个新的对象in01.a = 1; } @Testpublic void testDemain() { MyInner in = new MyInner(); in.a = 9; change(in); System.out.println(in.a); } }
实际输出:9
如果采用引用传递,传递完成以后,in01指向in,对in01的操作就是对in的操作,in01指向对象2,那么in也指向对象2,输出1,与实际不符,所以不是采用引用传递。
不再一一分析其他变量类型,分析后可以发现,java在传递参数时采用的不是引用传递,而是值传递。
简单讲,值传递时方法内外是两个拥有同一指向的变量,引用传递时方法内外是同一个变量。
The above is the detailed content of What is parameter passing? What is his use?. For more information, please follow other related articles on the PHP Chinese website!

Start Spring using IntelliJIDEAUltimate version...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

Java...

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to set the SpringBoot project default run configuration list in Idea using IntelliJ...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

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.

WebStorm Mac version
Useful JavaScript development tools

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),

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.