Home  >  Article  >  Java  >  Modify function incoming values ​​in Java: basic types and reference types

Modify function incoming values ​​in Java: basic types and reference types

php是最好的语言
php是最好的语言Original
2018-08-04 11:57:373446browse

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.

Modify function incoming values ​​in Java: basic types and reference types

Modify function incoming values ​​in Java: basic types and reference types

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(&#39;e&#39;, &#39;o&#39;)
     *         returns "mosquito in your collar"
     * "the war of baronets".replace(&#39;r&#39;, &#39;y&#39;)
     *         returns "the way of bayonets"
     * "sparring with a purple porpoise".replace(&#39;p&#39;, &#39;t&#39;)
     *         returns "starring with a turtle tortoise"
     * "JonL".replace(&#39;q&#39;, &#39;x&#39;) returns "JonL" (no change)
     * 
     *      * @param   oldChar   the old character.      * @param   newChar   the new character.      * @return  a string derived from this string by replacing every      *          occurrence of {@code oldChar} with {@code newChar}.      */     public String replace(char oldChar, char newChar) {         if (oldChar != newChar) {             int len = value.length;             int i = -1;             char[] val = value; /* avoid getfield opcode */             while (++i 

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn