当前位置:网站首页>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 **/
边栏推荐
- 机器学习:随机梯度下降(SGD)与梯度下降(GD)的区别与代码实现。
- Sword finger offer II 035 Minimum time difference - quick sort plus data conversion
- 从零开始匹配vim(0)——vimscript 简介
- Metauniverse urban legend 02: metaphor of the number one player
- uva 1401 dp+Trie
- AcWing 346. 走廊泼水节 题解(推公式、最小生成树)
- 736. Lisp 语法解析 : DFS 模拟题
- Supersocket 1.6 creates a simple socket server with message length in the header
- DS-5/RVDS4.0变量初始化错误
- Make Jar, Not War
猜你喜欢

系统休眠文件可以删除吗 系统休眠文件怎么删除

Asset security issues or constraints on the development of the encryption industry, risk control + compliance has become the key to breaking the platform

Yunna | work order management measures, how to carry out work order management

MySQL script batch queries all tables containing specified field types in the database

云呐-工单管理制度及流程,工单管理规范

2022 Google CTF SEGFAULT LABYRINTH wp

Wood extraction in Halcon

第三方跳转网站 出现 405 Method Not Allowed

2022 Google CTF segfault Labyrinth WP

Transplant DAC chip mcp4725 to nuc980
随机推荐
Share a general compilation method of so dynamic library
Neon Optimization: summary of performance optimization experience
How to evaluate load balancing performance parameters?
The difference between spin and sleep
一起看看matlab工具箱内部是如何实现BP神经网络的
Case development of landlord fighting game
微信公众号发送模板消息
7.6模拟赛总结
JS reverse -- ob confusion and accelerated music that poked the [hornet's nest]
系统休眠文件可以删除吗 系统休眠文件怎么删除
NEON优化:性能优化经验总结
2022 Google CTF SEGFAULT LABYRINTH wp
Your cache folder contains root-owned files, due to a bug in npm ERR! previous versions of npm which
Installation of gazebo & connection with ROS
Google发布安全更新,修复Chrome中已被利用的0 day
How to prevent overfitting in cross validation
table表格设置圆角
[chip scheme design] pulse oximeter
C language instance_ five
HMM notes