当前位置:网站首页>今日问题-2022/7/4 lambda体中修改String引用类型变量
今日问题-2022/7/4 lambda体中修改String引用类型变量
2022-07-06 17:46:00 【机智小袁】
说明:
1:lambda表达式的作用域中,对于局部引用变量是不做限制的。也就是可以在lambda体中对局部引用变量的值进行修改。原因是引用变量的值是保存在堆中的,是线程共享的所以Lambda可以修改他的值的。
遇到问题
String类型的变量是引用变量对吧,但是String类型的变量在lambda体中不允许修改。
public class Test {
static String staticStr = "静态变量";
String instanceStr = "实例变量";
public static void main(String[] args) {
/** * 遇到一个问题,String不算引用类型吗?为什么在lambda中修改String类型变量不成功。 */
int t = 0;
List<String> list = new ArrayList<>();
list.add("我是第0个元素");
String str = "我是局部引用变量";
WorkerInterface workerInterface = (a,b)->{
System.out.println("a:"+a+"b:"+b);
list.get(0);
list.add("eee");
//此处会报错
System.out.println(str);
int c = t+1;
System.out.println("c:"+c);
};
workerInterface.doSomeWork(3,4);
list.add("33");
str = str+"我改变了";
}
}
/** * 自定义函数式接口 * @author wangdawei */
@FunctionalInterface
public interface WorkerInterface {
/** * 一个抽象方法 */
public void doSomeWork(int a,int b);
}
猜测
可能于String类型的优化有关,String类型是维护有一个字符串常量池的,池中的字符串会进行复用,池中的字符串是线程公有的。
解决
在lambda中允许修改局部引用类型变量的数据,但是不允许修改引用的指向。又因为String类型是不可变的类型,也就是说如果你要修改String类型变量的值,那就是将当前引用指向了其它地址。也就是改变了引用的指向,所以是错误的。
应对
如果想要使用那可以使用可变字符串
public class Test {
static String staticStr = "静态变量";
String instanceStr = "实例变量";
public static void main(String[] args) {
/** * 遇到一个问题,String不算引用类型吗?为什么在lambda中修改String类型变量不成功。 */
int t = 0;
List<String> list = new ArrayList<>();
list.add("我是第0个元素");
String str = "我是局部引用变量";
WorkerInterface workerInterface = (a,b)->{
System.out.println("a:"+a+"b:"+b);
list.get(0);
list.add("eee");
stringBuffer.append("我变了");
System.out.println(stringBuffer.toString());
stringBuilder.append("我变了");
System.out.println(stringBuilder.toString());
int c = t+1;
System.out.println("c:"+c);
};
workerInterface.doSomeWork(3,4);
list.add("33");
}
}
/** *结果: *我是可变字符串StringBuffer我变了 *我是可变字符串stringBuilder我变了 *c:1 **/
边栏推荐
- 交叉验证如何防止过拟合
- 2022 Google CTF SEGFAULT LABYRINTH wp
- Can the system hibernation file be deleted? How to delete the system hibernation file
- Js逆向——捅了【马蜂窝】的ob混淆与加速乐
- 云呐|工单管理办法,如何开展工单管理
- golang中的WaitGroup实现原理
- 安全保护能力是什么意思?等保不同级别保护能力分别是怎样?
- ARM裸板调试之JTAG原理
- Tensorflow GPU installation
- Dark horse notes - exception handling
猜你喜欢

阿里云中mysql数据库被攻击了,最终数据找回来了

ESP Arduino (IV) PWM waveform control output

The MySQL database in Alibaba cloud was attacked, and finally the data was found

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

golang中的Mutex原理解析

boot - prometheus-push gateway 使用

Js逆向——捅了【马蜂窝】的ob混淆与加速乐

Do you understand this patch of the interface control devaxpress WinForms skin editor?

Segmenttree

资产安全问题或制约加密行业发展 风控+合规成为平台破局关键
随机推荐
[Niuke] [noip2015] jumping stone
Neon Optimization: performance optimization FAQ QA
Oracle: Practice of CDB restricting PDB resources
Oracle:CDB限制PDB资源实战
Can the system hibernation file be deleted? How to delete the system hibernation file
Boot - Prometheus push gateway use
THREE.AxesHelper is not a constructor
2022 Google CTF SEGFAULT LABYRINTH wp
Installation and testing of pyflink
系统休眠文件可以删除吗 系统休眠文件怎么删除
Google发布安全更新,修复Chrome中已被利用的0 day
「笔记」折半搜索(Meet in the Middle)
The difference between spin and sleep
mysql: error while loading shared libraries: libtinfo. so. 5: cannot open shared object file: No such
UI控件Telerik UI for WinForms新主题——VS2022启发式主题
Your cache folder contains root-owned files, due to a bug in npm ERR! previous versions of npm which
JTAG debugging experience of arm bare board debugging
C语言实例_5
Taro 小程序开启wxml代码压缩
C# 计算农历日期方法 2022