当前位置:网站首页>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 **/
边栏推荐
- Case development of landlord fighting game
- THREE. AxesHelper is not a constructor
- Neon Optimization: an optimization case of log10 function
- docker 方法安装mysql
- Lldp compatible CDP function configuration
- Gnet: notes on the use of a lightweight and high-performance go network framework
- 修改px4飞控的系统时间
- The MySQL database in Alibaba cloud was attacked, and finally the data was found
- Yunna - work order management system and process, work order management specification
- Transplant DAC chip mcp4725 to nuc980
猜你喜欢
1123. 最深叶节点的最近公共祖先
Yunna | work order management software, work order management software app
Lldp compatible CDP function configuration
【C语言进阶篇】指针的8道笔试题
身体质量指数程序,入门写死的小程序项目
MySQL script batch queries all tables containing specified field types in the database
Let's see through the network i/o model from beginning to end
JTAG debugging experience of arm bare board debugging
Transformation transformation operator
子网划分、构造超网 典型题
随机推荐
黑马笔记---异常处理
AcWing 346. 走廊泼水节 题解(推公式、最小生成树)
树莓派/arm设备上安装火狐Firefox浏览器
从零开始匹配vim(0)——vimscript 简介
uva 1401 dp+Trie
[advanced C language] 8 written questions of pointer
Yunna - work order management system and process, work order management specification
子网划分、构造超网 典型题
shell脚本快速统计项目代码行数
HMM 笔记
移植DAC芯片MCP4725驱动到NUC980
Docker method to install MySQL
C language instance_ five
Realize incremental data synchronization between MySQL and ES
Dark horse notes - exception handling
Gnet: notes on the use of a lightweight and high-performance go network framework
Typical problems of subnet division and super network construction
Gazebo的安装&与ROS的连接
图片打水印 缩放 和一个输入流的转换
云呐-工单管理制度及流程,工单管理规范