Java 变量参数传入方法,修改后是否影响外面的值

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

    }
}

如上面代码实践,结果输出 

s
null

原因是方法在执行的时候有栈帧的概念,入栈的时候只是压栈方法参数是传入参数的副本。

JVM高级特性

此时区分数据类型:基本类型和引用类型

基本类型:值存放在局部变量表中,无论如何修改只会修改当前栈帧的值,方法执行结束对方法外不会做任何改变;此时需要改变外层的变量,必须返回主动赋值。

引用数据类型:指针存放在局部变量表中,调用方法的时候,副本引用压栈,赋值仅改变副本的引用但是如果直接改变副本引用的值,修改了引用地址的对象,此时方法以外的引用此地址对象当然被修改。(两个引用,同一个地址,任何修改行为2个引用同时生效)

比如

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()); //输出sss

    }

关于String,本质是final类型char数组,不可修改,只能赋值,在做参数传入方法修改时,其实是新建对象,必须返回重新对外面的变量赋值才会对外面的String引用生效。

看String源码的任意一个方法即可明白

/**
     * 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>
     * "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)
     * </pre></blockquote>
     *
     * @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++;
                }
                //String的value 是final char[],一旦创建不能改变
                return new String(buf, true);
            }
        }
        return this;
    }

引用类型会引起浅拷贝和深拷贝现象。

  • 17
    点赞
  • 48
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
Java 中,当你将一个 int 类型的变量传入一个方法中时,实际上是将该变量传递给了该方法。在方法中对该变量进行修改时,只会修改方法内部的局部变量,而不会对原变量产生影响。 如果想要在方法内部修改变量,可以使用 Java 中的引用传递或者返回的方式。具体来说,可以将该变量封装成一个对象,并将对象作为参数传递给方法,或者让方法返回一个新的并将其赋给原变量。 例如,可以定义一个包含 int 类型变量的简单类,并在该类中编写修改变量方法,然后将该类的实例作为参数传递给需要修改变量方法。这样,在方法中对该类的实例进行操作时,就可以修改变量。例如: ```java class IntHolder { private int value; public IntHolder(int value) { this.value = value; } public int getValue() { return value; } public void setValue(int value) { this.value = value; } } public class Main { public static void main(String[] args) { IntHolder holder = new IntHolder(10); modifyInt(holder); System.out.println(holder.getValue()); // 输出 20 } public static void modifyInt(IntHolder holder) { holder.setValue(holder.getValue() * 2); } } ``` 在上面的例子中,我们定义了一个 IntHolder 类来封装一个 int 类型的变量,并在 modifyInt 方法修改了该变量。在 main 方法中,我们创建了一个 IntHolder 对象,并将其作为参数传递给 modifyInt 方法。在 modifyInt 方法修改了该对象的后,我们再次访问该对象的,发现其已经被成功地修改了。
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值