In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how is parameter transfer in Java". In daily operation, I believe many people have doubts about how parameter transfer is in Java. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubt of "how parameter transfer is in Java". Next, please follow the editor to study!
Preface
Parameter passing in Java: value passing and reference passing
But in essence, only values are passed in Java. Reference passing can actually be understood as passing something similar to a pointer.
Value passing is to make a copy of the value of the basic variable and pass that copy. Reference passing is the address of the passed reference, that is, the address of the variable in memory space.
1. Value transfer
Only the basic data type uses value transfer, which is characterized by the transfer of a copy of the value, after which there is no relationship between the two. That is to say, the values inside and outside the method are independent of each other.
Basic data types:
Integer: int,long,byte,short
Floating point: float,double
Character type: char
Boolean: boolean
Note: all data types other than the 8 basic data types are reference types.
two。 Reference transfer
It means that when a method is called, the parameters passed are passed by reference, but in fact, the address of the reference passed is the address of the memory space corresponding to the variable.
What is transmitted is a copy, that is, a copy. That is, for a parameter pass, there are two addresses that point to the same memory space. Here we can show it with a schematic diagram of memory allocation.
3.String type passing
Let's first come to the conclusion that the effect of String type passing is similar to that of basic data types.
Description:
Once a String class object is created, its contents cannot be changed:
All the methods of the String class do not change the content of the string class object, and to change the value of the string class object, you must create a new String object.
That is, when parameters are passed, if the value of the String class object is modified within the method, a new String class object is actually created and the original variable points to it. But this "original variable" is a copy, but it was created with the same value passed in the main method, and now it has nothing to do with it.
4. Give an example
1) Code
Public class TestTransOfValue {public static void main (String args []) {doubleval; StringBuffer sb1, sb2; String sb3; char s [] = {'a 'axie'}; sb1 = new StringBuffer ("apples"); sb2=new StringBuffer ("pears"); sb3 = new String ("pear"); modify (val, sb1, sb2,sb3,s) System.out.println (val); System.out.println (sb1); System.out.println (sb2); System.out.println (sb3); System.out.println (s);} public static void modify (double a, StringBuffer R1, StringBuffer R2) string r3 language char s []) {a = 6.8; r1.append ("taste good"); r2=null R3 = "banana"; s [2] = 'Rises;}}
2) running result
5.8
Apples taste good
Pears
Pear
ApRle
3) explain:
① val is a basic data type, and the two copies of values directly do not affect each other. So the operation on a within the modify method does not affect the value of the val of the main method, so val=5.8
② sb1 is StringBuffer and is not a basic type, so it is reference passing. R1.append () modifies the value of the memory space corresponding to the R1 address, so the value of sb1 changes
③ sb2 is also StringBuffer and belongs to reference passing. However, r2=null is the value of the address of the modified R2 rather than the value of the memory space that the R2 address points to, so the sb2 points to the original memory space, and the value of the memory space has not been changed.
④ sb3 is of type String and belongs to reference passing. But the String type is a special class, and changing the value of String in the method does not change the value of String in the main method, so String is still pear.
The ⑤ char array is passed by reference, and s [2] = 'char, which does modify the value of the memory space, so the value of the array is changed
At this point, the study of "what is the parameter transfer in Java" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.