当前位置:网站首页>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 .
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 **/
边栏推荐
- 736. Lisp 语法解析 : DFS 模拟题
- According to the analysis of the Internet industry in 2022, how to choose a suitable position?
- C语言实例_2
- NEON优化:关于交叉存取与反向交叉存取
- Realize incremental data synchronization between MySQL and ES
- ZOJ Problem Set – 2563 Long Dominoes 【如压力dp】
- 2022 Google CTF SEGFAULT LABYRINTH wp
- Instructions for using the domain analysis tool bloodhound
- How to prevent overfitting in cross validation
- Taro中添加小程序 “lazyCodeLoading“: “requiredComponents“,
猜你喜欢
The MySQL database in Alibaba cloud was attacked, and finally the data was found
454-百度面经1
According to the analysis of the Internet industry in 2022, how to choose a suitable position?
Force buckle 1037 Effective boomerang
Comparison of picture beds of free white whoring
黑马笔记---创建不可变集合与Stream流
Body mass index program, entry to write dead applet project
JTAG principle of arm bare board debugging
AcWing 361. 观光奶牛 题解(spfa求正环)
Yunna | work order management software, work order management software app
随机推荐
[advanced C language] 8 written questions of pointer
[signal and system]
移植DAC芯片MCP4725驱动到NUC980
Sword finger offer II 035 Minimum time difference - quick sort plus data conversion
云呐|工单管理软件,工单管理软件APP
[JS] obtain the N days before and after the current time or the n months before and after the current time (hour, minute, second, year, month, day)
Neon Optimization: an instruction optimization case of matrix transpose
7.6模拟赛总结
C language instance_ three
Taro applet enables wxml code compression
Analysis of mutex principle in golang
LeetCode:1175. 质数排列
Gazebo的安装&与ROS的连接
修改px4飞控的系统时间
How to prevent overfitting in cross validation
Oracle: Practice of CDB restricting PDB resources
736. LISP syntax parsing: DFS simulation questions
第三方跳转网站 出现 405 Method Not Allowed
go-zero微服务实战系列(九、极致优化秒杀性能)
uva 1401 dp+Trie