Basic type and reference type Basic type: The value is stored in the local variable table. No matter how modified, it will only modify the value of the current stack frame. After the method is executed, no changes will be made outside the method; at this time, the outer layer needs to be changed. Variables must return active assignments. Reference data type: The pointer is stored in the local variable table. When the method is called, the copy reference is pushed onto the stack, and the assignment only changes the reference of the copy. However, if the value of the copy reference is directly changed and the object of the reference address is modified, then of course the object referencing this address outside the method will be modified. (Two references, the same address, any modification behavior takes effect on both references at the same time)
public class Test2 { public static void setValue(String str){ str = "ss"; } public static void setValue(Man str){ str = new Man("test"); } public static class Man{ private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public Man(String name) { this.name = name; } @Override public String toString() { return "Man{" + "name='" + name + '\'' + '}'; } } public static void main(String[] args) { String str = "s"; setValue(str); System.out.println(str); Man man = null; setValue(man); System.out.println(man); } }
As shown in the above code practice, the result output
s null
The reason is that the method has The concept of stack frame, when pushing onto the stack, is that the push method parameters are copies of the incoming parameters.
Java Advanced Features
Distinguish data types at this time: basic types and reference types
Basic type: The value is stored in the local variable table. No matter how you modify it, it will only modify the value of the current stack frame. After the method is executed, no changes will be made outside the method; at this time, you need To change the outer variable, active assignment must be returned.
Reference data type: The pointer is stored in the local variable table. When the method is called, the copy reference is pushed onto the stack, and the assignment only changes the reference of the copy. But if you directly change the value of the copy reference and modify the object of the reference address, then of course the object referencing this address outside the method will be modified. (Two references, the same address, any modification takes effect on both references at the same time)
For example,
public static void setValue(StringBuilder str){ str = new StringBuilder("sss"); } public static void setValue2(StringBuilder str){ str.append("sss"); } public static void main(String[] args) { StringBuilder str = new StringBuilder(); setValue(str); System.out.println(str.toString()); setValue2(str); System.out.println(str.toString()); }
Regarding String, it is essentially a final type char array and cannot be modified. , can only assign values. When modifying the parameters passed in to the method, it is actually a new object. You must return and re-assign the external variables to take effect on the external String reference.
You can understand by looking at any method of the String source code
/** * Returns a string resulting from replacing all occurrences of * {@code oldChar} in this string with {@code newChar}. * <p> * If the character {@code oldChar} does not occur in the * character sequence represented by this {@code String} object, * then a reference to this {@code String} object is returned. * Otherwise, a {@code String} object is returned that * represents a character sequence identical to the character sequence * represented by this {@code String} object, except that every * occurrence of {@code oldChar} is replaced by an occurrence * of {@code newChar}. * <p> * Examples: * <blockquote><pre class="brush:php;toolbar:false"> * "mesquite in your cellar".replace('e', 'o') * returns "mosquito in your collar" * "the war of baronets".replace('r', 'y') * returns "the way of bayonets" * "sparring with a purple porpoise".replace('p', 't') * returns "starring with a turtle tortoise" * "JonL".replace('q', 'x') returns "JonL" (no change) *
The reference type will cause shallow copy and deep copy phenomena.
Related articles:
How to modify variables outside the scope in JavaScript
PHP closure function Methods of passing parameters and using external variables, php variables
The above is the detailed content of Modify function incoming values in Java: basic types and reference types. For more information, please follow other related articles on the PHP Chinese website!