What is parameter passing? What is his use?
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);
}
Copy after login
Actual output: 9public void change(int b) { b = 7; } @Testpublic void testBasic() {int a = 9; change(a); System.out.println(a); }
When 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);
}
Copy after login
Actual output: www.baidu.com
public void change(String str01) { str01 = "baidu"; } @Testpublic void testString() { String str = new String("www.baidu.com"); change(str); System.out.println(str); }
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);
}
Copy after login
Actual output: Hello World!
public void change(StringBuilder builder01) { builder01.append(" World!"); } @Testpublic void testStringBuilder() { StringBuilder builder = new StringBuilder("Hello"); change(builder); System.out.println(builder); }
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);
}
}
Copy after login
Actual output: 1
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); } }
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!

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.
