In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "what is Java delivery". In daily operation, I believe many people have doubts about what Java is. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts about "what is Java?" Next, please follow the editor to study!
The answer is revealed at the beginning. In the Java language, there is only value transmission in essence, but no citation transmission. Please see the text for explanation and proof.
When it comes to value passing and reference passing, we have to mention two concepts: value types and reference types.
1. Value Typ
In a popular sense, the so-called value types refer to the eight basic data types in Java:
Integer type: byte, int, short, long
Floating point type: float, double
Character type: char
Boolean type: boolean
From the JVM level: the so-called value type refers to the type that generates the value directly in the stack (Java virtual machine stack) when assigning a value.
two。 Reference type
Reference types are data types other than value types, such as:
Class
Interface
Array
String
Wrapper class (Integer, Double...)
At the JVM level, the so-called reference types are those data types that are referenced on the stack and values generated on the heap during initialization.
3. Value transfer
Value passing (Pass By Value) means that when a method passes parameters, it passes a copy of the original content, so how to modify the copy will not affect the original content.
The implementation code is as follows:
Public class PassTest {public static void main (String [] args) {int age = 18; System.out.println ("before calling method:" + age "); intTest (age); System.out.println (" after calling method: "+ age);} private static void intTest (int age) {age = 30; System.out.println (" modified to: "+ age in method);}}
The execution result of the program is:
Before calling the method: 18
Method is changed to: 30
After calling the method: 18
From the above results, we can see that the modification of parameters in the method does not affect the original content, and we call this way of parameter transfer value transfer.
4. Reference transfer
Pass By Reference means that when a method passes parameters, it passes the parameters themselves, so any modification to the parameters will affect the original content.
The implementation code to simulate "reference passing" is as follows:
Public class PassTest {public static void main (String [] args) {char [] name = {'programming', 'teacher'}; System.out.println ("before calling method:" + new String (name)); paramTest (name); System.out.println ("after calling method:" + new String (name));} private static void paramTest (char [] n) {n [1] = 'Lion' System.out.println ("method modified to:" + new String (n));}}
The execution result of the program is:
Before invoking the method: programmer
Method is modified to:
After calling the method:
From the above results, we can see that after the parameter is modified in the paramTest method, when the parameter is printed in the main method, it is found that the value of the parameter has also changed, so it seems that there seems to be a "reference pass" in Java, but this is not the case. Let's move on.
5. True and false "citation transmission"
Let's add a line to the above code, as follows:
Public class PassByValue {public static void main (String [] args) {char [] name = {'Lei', 'brother'}; System.out.println ("before calling method:" + new String (name)); paramTest (name); System.out.println ("after calling method:" + new String (name));} private static void paramTest (char [] n) {n = new char [2] / / add this line of code n [1] = 'God'; System.out.println ("modified in the method to:" + new String (n));}}
The execution result of the program is:
Before calling the method: brother Lei
Method is changed to: God
After calling the method: brother Lei
From the above results, we can see that when we add new char [] to the paramTest method, "reference passing" suddenly changes the value. Why?
This is because, in the Java language, there is essentially only value passing, which means that passing parameters to Java only passes a copy of it, not the parameters themselves.
The quoted "reference pass" in front of it just passes a copy of its reference.
So after we call new char [], we can see that the n object has a new address, but the original content has not been modified. If we look at it from the perspective of reference passing, any modification will change the original content, so we can be more sure that only values are passed in the Java language.
At this point, the study of "what is the transmission of 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.