当前位置:网站首页>Today's question -2022/7/4 modify string reference type variables in lambda body

Today's question -2022/7/4 modify string reference type variables in lambda body

2022-07-07 01:30:00 Witty little yuan

explain :

1:lambda In the scope of the expression , There are no restrictions on local reference variables . That is to say, it can be in lambda Body to modify the value of the local reference variable . The reason is that the value of the reference variable is stored in the heap , It is shared by threads, so Lambda You can modify his value .

Have a problem

String Variables of type are reference variables, right , however String The variables of type are lambda Body cannot be modified .

public class Test {
    
    static String staticStr = " Static variables ";
    String instanceStr = " Instance variables ";
    public static void main(String[] args) {
    
		/** *  There's a problem ,String Not a reference type ? Why is it lambda Revision in China String Type variable failed . */
        int t = 0;
        List<String> list = new ArrayList<>();
        list.add(" I am the first 0 Elements ");
        String str = " I am a local reference variable ";
        WorkerInterface workerInterface = (a,b)->{
    
            System.out.println("a:"+a+"b:"+b);
            list.get(0);
            list.add("eee");
            // An error will be reported here 
            System.out.println(str);
            int c = t+1;
            System.out.println("c:"+c);
        };
        workerInterface.doSomeWork(3,4);
        list.add("33");
        str = str+" I changed ";
   }
}

/** *  Custom functional interface  * @author wangdawei */
@FunctionalInterface
public interface WorkerInterface {
    
    /** *  An abstract method  */
    public void doSomeWork(int a,int b);
}

guess

It may be in String Type optimization ,String Types maintain a string constant pool , Strings in the pool will be reused , Strings in the pool are public to threads .

solve

stay lambda It is allowed to modify the data of local reference type variables , However, it is not allowed to modify the reference point . Again because String Types are immutable types , That is, if you want to modify String The value of the type variable , That is to point the current reference to another address . That is to change the reference point , So it's wrong .
 Insert picture description here

Answer

If you want to use it, you can use a variable string

public class Test {
    
    static String staticStr = " Static variables ";
    String instanceStr = " Instance variables ";
    public static void main(String[] args) {
    
		/** *  There's a problem ,String Not a reference type ? Why is it lambda Revision in China String Type variable failed . */
        int t = 0;
        List<String> list = new ArrayList<>();
        list.add(" I am the first 0 Elements ");
        String str = " I am a local reference variable ";
        WorkerInterface workerInterface = (a,b)->{
    
            System.out.println("a:"+a+"b:"+b);
            list.get(0);
            list.add("eee");
            stringBuffer.append(" I have changed ");
            System.out.println(stringBuffer.toString());
            stringBuilder.append(" I have changed ");
            System.out.println(stringBuilder.toString());
            int c = t+1;
            System.out.println("c:"+c);
        };
        workerInterface.doSomeWork(3,4);
        list.add("33");
   }
}
/** * result : * I am a variable string StringBuffer I have changed  * I am a variable string stringBuilder I have changed  *c:1 **/

原网站

版权声明
本文为[Witty little yuan]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207061746080074.html