Home > Java > javaTutorial > body text

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

php是最好的语言
Release: 2018-08-04 11:57:37
Original
3465 people have browsed it

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);

    }
}
Copy after login

As shown in the above code practice, the result output

s
null
Copy after login

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());

    }
Copy after login

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)
     * 
Copy after login
* * @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 < len) { if (val[i] == oldChar) { break; } } if (i < len) { char buf[] = new char[len]; for (int j = 0; j < i; j++) { buf[j] = val[j]; } while (i < len) { char c = val[i]; buf[i] = (c == oldChar) ? newChar : c; i++; } return new String(buf, true); } } return this; }

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template