In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you what are the types of java reference transmission, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
The first of the three types passed by java references
Result: before calling: 50
After calling: 1000
Analysis:
Understand: easy to understand
The second mode of transmission
Let's look at the example first.
Running result:
Analyze the picture:
The third mode of transmission
Results:
Analysis:
Understanding of three kinds of reference transmission
Both the first and the third are easy to understand:
In fact, the address is passed in the c language, of course, the attribute value can be modified. For the second kind, it is because the String class is special. In the second example, the fun () function str2= "mldn" is actually an anonymous object! This equation actually changes the address value of the reference to str2, even if the reference address of str1 points to mldn, the object in heap memory.
The problem of passing value by java reference
One picture is worth ten thousand words (with a picture of the ship launching god, a hanging god written by a custom control):
I have been working on a project these days. Sure enough, it is no good to only engage in theory. It has been nearly half a year since the last project was written. Today, both efficiency and proficiency are not as good as before.
Well, to get to the point, the problem I'm going to talk about today is actually very simple-- the problem of parameter passing in java. In fact, I admit that in this place I just know that objects pass references, common types pass values, and typical theories. (+), but this problem can be big or small, and I think it's better to keep it clear.
The origin of the problem, a stupid case is a failure.
As a matter of fact, writing this article today is a complete call. +, just because of the problem when I was working on the universal adapter of RecyclerView, let's introduce the scene at that time:
@ Override public void resultCallbackFromFragment (List list) {Toast.makeText (this, "modified successfully!" , Toast.LENGTH_SHORT). Show (); ContactList = list; adapter.notifyDataSetChanged ();. }
Only the code we designed is left, and all the other parts of the code are typed. Yes. Next, I'll introduce the scenario in an extremely simple organizational language:
Open an interface with a check box and return the selected data when you exit. The method is a callback method, and the effect of the method is to update the list data (contactList is the source data that we passed into RecyclerView).
In theory, update contactList to get the latest value, then call the notifyDataSetChanged method, and the list is refreshed, and everything looks so perfect. Then let's take a look at the effect:
Don't complain about this App background, because it's for my little fairy. +!
In the above effect, we can see that after selecting two contacts and clicking OK, it should be displayed as two people, how is it still the data just now?
At that time, I also knew that the transmission of the reference type passed the reference, and I recalled my idea at that time: the reference was passed to another reference, and the content of this reference changed, and everything changed. Some of my friends may find it funny to see this sentence: wow blogger, you are so bad, such basic questions have been bypassed. Well, I have to admit that the foundation of java is a little poor.)
It was such a simple sentence that I spared several big bends. At that time, I was already involved. I felt that the data had been changed, and then I began to look for mistakes elsewhere. It took a long time to reflect on whether there was something wrong with the data transmission process. +
And then I started looking for issues related to parameter passing, all right, let's jump out of the above case, and I don't want you to be affected by the fancy things above, because there's only one problem we're talking about today: the reference value of java.
Two types of parameter transfer
Parameter transfer is mainly divided into two types: one is that the parameter is the basic type, and the other is that the parameter is the reference type.
Basic data type
I believe there is nothing wrong with this. When the basic type is passed as a parameter, it opens up a new memory in a method stack and copies the original data value, so no matter how we modify it, the original data value will not be affected in any way.
Take a simple chestnut:
Public class Practice2 {public static void main (String [] args) {/ / TODO Auto-generated method stub int a = 5; System.out.println (a); change (a); System.out.println (a);} public static void change (int b) {b = 500;}}
The results are as follows:
five
five
Nothing's changed, is it?
Reference data type
First of all, we need to know that the referenced data is stored in stack memory, while the object pointed to by the reference is stored in heap memory.
When a reference is passed to a method as a method parameter, it copies the value of the reference to another reference, but the references all point to the same heap memory, so the modification is equally valid.
Example code:
Public class Practice {static An a = new A (10); public static void main (String [] args) {/ / TODO Auto-generated method stub Practice practice = new Practice (); System.out.println (practice.a.intData); change (practice.a); System.out.println (practice.a.intData) } public static void change (An aa) {aa.intData = 500; System.out.println (aa.intData);}} class A {int intData; public A (int intData) {this.intData = intData;}}
ten
five hundred
It's not difficult to say that, is it?
Reference transfer
In fact, the reference parameter passing mentioned above is essentially the passing of a reference, and we pass the reference to another reference, so both references have the same value-both pointing to the same object.
An A1 = new A (10); An a2 = A1 intData system. Out.println ("intData of A1:" + a1.intData + "intData of a2:" + a2.intData); a2.intData = 500 × System.out.println ("intData of A1:" + a1.intData + "intData of a2:" + println)
The results are as follows:
A1 intData: 10 a2 intData: 10
A1 intData: 500a2 intData: 500
Note: in reference types, formal parameters can change the value of arguments, or one reference can change the value of another reference, simply because they have the same value stored in stack memory, but this value can be changed at any time.
This is where I have been trapped before, in fact, as long as the reference stored value has changed, the two references have nothing to do with each other. See the following example:
An A1 = new A (); An a2 = A1 System.out.println (A1); System.out.println (a2); a2 = new A (); System.out.println (A1); System.out.println (a2)
The results are as follows:
Aids 33909752
Aids 33909752
Aids 33909752
A@55f96302
After a2 points to the new object, A1 and a2 no longer have any relationship, because the values stored by their two references are completely different.
I believe this picture has made it very clear.
In turn, solve the case.
Now that we have the above theoretical knowledge, we are looking back at the problem at the beginning.
@ Override public void resultCallbackFromFragment (List list) {Toast.makeText (this, "modified successfully!" , Toast.LENGTH_SHORT). Show (); ContactList = list; adapter.notifyDataSetChanged ();. }
After we get the new list, we assign a new reference to contactList, which points to a new heap memory space. But the list in the adapter still points to the previous reference, because we just change the value of the contactList reference and then execute the notifyDataSetChanged method, but the list data in the adapter is still the data that the original contactList points to.
So the solution is to directly change the list reference in the adapter, and then call the notifyDataSetChanged method:
Public void notifyData (List mList) {this.mList = mList; notifyDataSetChanged ();}
Just write a method to modify the data in the adapter and call it outside:
@ Override public void resultCallbackFromFragment (List list) {Toast.makeText (this, "modified successfully!" , Toast.LENGTH_SHORT). Show (); ContactList = list; adapter.notifyData (contactList);. } the above is all the content of the article "what are the types of java references?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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