Background (#)
Recently received the question raised by the former brother, that is, Java in the end is the reference or value (according to his argument and arguments), actually not as explained, because I think it is a very simple concept, when you see something to explain
No, what you should do is what the data is doing in Java memory, so the advice I give him is to look at what's in memory, and explain it to yourself today.
(#) Example
Looking at the small example above, first of all, what do you think this is going to pass? Value does not seem to be all, the reference seems to explain, in fact, this time is very simple, we analyze the two memory structure,
(1) for the basic type of int, we know that its storage model is such as int a=0, which is stored directly inside the stack, and what happens when we call T1 ()?
The value of a, B, is directly assigned to the variable, a, B, in the stack, so it is obvious that the A, B, and A/A in the main method are not related, which is the reason for the first case
(2) For string type, the first thing to say is that the string type is not the basic type, then we string str= "123" When the memory of what happened? To see
So what happens when we call the T2 () method, the value is passed, it's simple, we just pass the stored value in, and what's the value? It's easy to understand.
So what happened when str= "Java"?
Yes, str= "Java" is actually the equivalent of Str=new String ("Java"), then we actually re-allocated memory in the heap, and then reference re-points to the newly allocated memory, so of course it does not affect the outside
(3) and similarly to the T3 method, we pass in the value, but this value is the address stored in the stack, the flowchart is as follows
Then the rest is fine. The reason for the change to take effect is because the two points to the same heap of memory, so the modification is normal, so for example, if I write a line in T3,
T=new Testurl (); Does this affect Testurl??? Think about what it means if you can quickly think of what's in memory, and you actually know that Java only passes the value.
is Java a value or a pass-through reference?