[JAVA rookie] ask questions about the scope of variables

the topics are as follows:

bold text clipboard.png

clipboard.png

Standard answer choose B
I was thinking, why can"t the change method in this change the value of str? Is
because local variables cannot modify global variables?
the problem is the above

String str = new String"good";
There is no static in this sentence. I am dizzy, didn"t I say that the one with static is the global variable?
beginners, do not understand, the problem is relatively simple, please forgive me.

Apr.21,2021

this problem has nothing to do with the function, mainly the parameter passing of Java.

new String ("good") No problem, it's a string. The
problem occurs when calling change. The parameters are str and ch [] .
Java there is no reference passing, only values are passed, but (note this but), an object and an array as parameters will get a copy of the referenced values of the object and array.

this sentence may be a little difficult to understand, let's split it like this.

first, String str = new String ("good"); at this time we create a string object whose content is "good", but this object is not str , str is just a reference to this object. In the same way, we create an array ch , and ch is not this array, but a reference to this array.

this is why we can do str = "abc" without reporting an error (the String object is immutable), because we didn't modify the object, we just created a new "abc" String, and modified the reference to point to the new string object.

next, let's take a look at passing parameters. Again, the parameter passing of Java is value passing.

before we call, we have a str and a ch , which are references to objects and arrays, respectively.
pass parameters next. JVM makes a copy of our str and ch , which I write down here as _ str and _ ch . Because it is a copy of the value
, the copied _ str and _ ch are the same as the original, and is also a reference .

so this is the key part. Then, inside the function, the reference _ str is redirected to a new String object (so the original str has no effect). Next, through _ ch (which, like the original ch , is a reference to that array), we directly modify the 0 element of that array, which causes the memory of the original array to be modified.

so in the end, the result is that, in the original function, when the values pointed to by str and ch are checked, the reference itself and the object he points to remain the same, so str remains the same, while the array pointed to by ch is modified (the copied reference points to the real address).

so choose B.
(you don't have a tool on hand, otherwise you can draw a picture to show it. Here is a description of , which you can learn about)

.

so this problem has nothing to do with static, it is a reflection on the type of parameters passed by Java functions and variables.


this is not a question of variable scope, but about the problem when the method passes parameters to a reference type. Str= "test ok" is to directly let the formal parameter point to a new object, but the ex.str of the argument does not change the original direction, while ch [0] ='g 'modifies the internal properties of ch, the char array object (the array is also an object), so the ex.str has not changed and ex.ch has changed after the end of the method. In more detail upstairs


can you replace the str = "test ok"; in the change method with this.str = "test ok"; feel it?

Menu