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 > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
Java value transfer how to understand, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.
1. Basic concepts:
Value passing and reference passing refers to whether the parameter passed by the caller is a specific value or an address reference in a method call. I found that my partner believes that the biggest reason for reference passing in Java is a misunderstanding of the "address" in the above concept, which refers to the reference of variables in the stack, not the address of objects in the heap.
Value passing: as the name implies, the actual value is passed to the method from the place where the method is called. In other words, I give you my value, and you can change it as much as you want, but any change you make will not affect me.
Reference passing: reference passing is a special kind of variable that is considered to be an alias for a variable. When defining a reference, it is actually an alias for the target variable, the reference does not allocate independent memory space, it shares its memory space with the target variable, and when defining a reference, if the reference is not used as a function parameter or return value, you must provide the initial value of the reference (that is, you must provide the target variable name of the reference).
Second, how can only values be passed in Java?
In the eight basic data types in Java and String, this value passing is easy to understand, as shown in the following code:
Public static void main (String [] args) {
Int a = 1
System.out.println ("before method call:" + a)
Sum (a)
System.out.println ("after method call:" + a)
}
Private static void sum (int a) {
A = 2
}
The output is (this is to be expected):
Before the method call: 1
After the method is called: 1
Many friends have the following idea: the object in Java is created in the heap, and the variable in the stack is just an address reference, so what is passed when the method is called is actually the address reference of the object in the heap. So they think that in Java, objects are just reference passing. Actually, no, please read the concept of reference passing carefully, it means that what is passed is the reference of the variable rather than the reference of the object, that is, the argument passed in the past is the reference of the variable in the stack. But in fact, when the method is called, it passes a copy of this variable and a copied value, but the copied value is the address of the object pointed to or that one, so when you modify this object, it actually modifies the value of the object in the heap, so it will change. See the following example:
Student object (standard JavaBean)
Class Student {
Private String name
Private int age
Public Student (String name, int age) {
This.name = name
This.age = age
}
Public Student () {
}
Public String getName () {
Return name
}
Public void setName (String name) {
This.name = name
}
Public int getAge () {
Return age
}
Public void setAge (int age) {
This.age = age
}
@ Override
Public String toString () {
Return "Student {" +
"name='" + name +'\'+
", age=" + age +
'}'
}
}
Testing method
Public static void main (String [] args) {
Student stu1 = new Student ("zhangsan", 12)
System.out.println ("before method call:" + stu1.toString ())
Change (stu1)
System.out.println ("after method call:" + stu1.toString ())
}
The most important thing is the change () method. It is difficult for us to prove that it is value passing from the front, but we can prove from the negative that it is not reference passing:
Private static void change (Student stu1) {
Stu1.setAge (22)
}
There is no problem with this input:
Before method call: Student {name='zhangsan', age=12} after method call: Student {name='zhangsan', age=22}
Private static void change (Student stu1) {
Stu1 = new Student ()
Stu1.setAge (99)
}
According to your experience, this output must be:
Before the method call: Student {name='zhangsan', age=12}
After method call: Student {name='zhangsan', age=12}
So what's the point? Why is this so? if it is a reference pass, then I will new it a new object here, and the variable in the main method should also be a new object, because I have changed its reference address, which means that this is not a reference pass, because in reference passing, when you change the value of the parameter, the argument of the calling method should also change.
What are the characteristics of Java? what are the characteristics of Java? what is the 1.Java language that represents the static object-oriented programming language? it implements the object-oriented theory and allows programmers to carry out complex programming in an elegant way of thinking. 2.Java has the characteristics of simplicity, object-oriented, distributed, security, platform independence and portability, dynamic and so on. 3. Using Java, you can write desktop applications, Web applications, distributed systems, embedded system applications, and so on.
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, 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.