In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the knowledge of "how to understand java value transfer and reference transfer". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Value transfer
During the call of the method, the argument passes its actual value to the parameter, which is passed by copying the value of the parameter to the function, so that if the value (the value of the parameter) is manipulated in the function, the value of the parameter will not be affected. Because of direct replication, this approach is particularly inefficient when transferring large amounts of data.
When the basic type of java is passed as an argument to the function body, the passed parameter becomes a local variable inside the function body, which is a copy of the input parameter. After all the operations within the function body are executed, the local variable completes its mission, and it does not affect the variable as a parameter.
Reference transfer
Reference transfer makes up for the deficiency of value transmission. If a large amount of data is passed, it will take up a lot of memory space, and reference transfer is to pass the address value of the object, and the function receives the first address value of the original value. During the execution of the method, the content of the parameter and the argument are the same, pointing to the same memory address, that is to say, the operation is actually the source data, so the execution of the method will affect the actual object.
Passing an object as an entry parameter in java defaults to "reference passing", passing only a reference to the object. When the body of the function changes the input variable, it is essentially a direct operation on the object.
Examples of value transfer:
Public static void main (String [] args) {
Int num1 = 10
Int num2 = 20
Swap (num1, num2)
System.out.println ("num1 =" + num1); / / 10
System.out.println ("num2 =" + num2); / / 20
}
Public static void swap (int a, int b) {
Int temp = a
A = b
B = temp
System.out.println ("a =" + a); / / 20
System.out.println ("b =" + b); / / 10
}
Application delivery example:
Public static void main (String [] args) {
Int [] arr = {1, 2, 3, 4, 5}
Change (arr)
System.out.println (arr [0]); / / 0
}
/ / change the first element of the array to 0
Public static void change (int [] array) {
Int len = array.length
Array [0] = 0
}
So much for the introduction of "how to understand java value passing and reference passing". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.