In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
Today, I will talk to you about whether it is value transfer or reference transfer in Java. Many people may not know much about it. In order to make you understand better, the editor summarized the following content for you. I hope you can get something according to this article.
Recently, I saw an interesting topic on stackoverflow. Is it "value passing" or "reference passing" in Java?
There are many answers and a lot of likes. It soon caught my attention!
Through my personal reading of the whole article, I got three common mistakes that were misled by everyone!
Error 1: value passing and reference passing, distinguishing between what is passed, and if it is a value, it is value passing. If it is a reference, it is a reference pass.
Error 2:Java is reference passing.
Error 3: if the parameter passed is a normal type, it is value passing, and if it is an object, it is reference passing.
In fact, there is only "value passing" in Java! This is not what my family said, but the result of the discussion of many netizens on stackoverflow!
I chose a more classic answer and tidied it up for you!
The most important point to understand is Dog myDog, which is actually a pointer to "Dog". When we call foo (myDog), you are actually passing the address of the created object Dog to the foo method.
Note that the pointer in Java does not refer to the address.
Now assume that the Dog object resides at memory address 42. This means that we pass 42 to the method.
The parameter someDog is set to a value of 42, and in the code "AAA", someDog follows Dog, which points to (the object at Dog address 42) and asks Dog (the one with address 42) to change his name to Max.
At the code "BBB", Dog creates a new. Assuming that its address is 74, we assign the parameter someDog to 74.
At the code "CCC", someDog follows Dog and points to (the object at Dog address 74), asking Dog (the one with address 74) to change his name to Rowlf.
Then, return up.
Java works in exactly the same way as C. You can assign pointers, pass pointers to methods, follow pointers in methods, and change the data you point to. However, you cannot change the location where the pointer points.
Java always passes parameters by value rather than by reference. Java passes the referenced address as a value, so you should understand!
After reading the above, do you have any further understanding of value passing or reference passing in Java? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
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.