当前位置:网站首页>今日问题-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 **/
边栏推荐
- Boot - Prometheus push gateway use
- Atomic in golang, and cas Operations
- "Exquisite store manager" youth entrepreneurship incubation camp - the first phase of Shunde market has been successfully completed!
- golang中的WaitGroup实现原理
- UI control telerik UI for WinForms new theme - vs2022 heuristic theme
- MySQL script batch queries all tables containing specified field types in the database
- NEON优化:矩阵转置的指令优化案例
- Make Jar, Not War
- 如何管理分布式团队?
- mysql: error while loading shared libraries: libtinfo.so.5: cannot open shared object file: No such
猜你喜欢
2022 Google CTF SEGFAULT LABYRINTH wp
Js逆向——捅了【马蜂窝】的ob混淆与加速乐
The MySQL database in Alibaba cloud was attacked, and finally the data was found
Make Jar, Not War
动态规划思想《从入门到放弃》
Send template message via wechat official account
Force buckle 1037 Effective boomerang
Come on, don't spread it out. Fashion cloud secretly takes you to collect "cloud" wool, and then secretly builds a personal website to be the king of scrolls, hehe
如何管理分布式团队?
Installation of gazebo & connection with ROS
随机推荐
[Niuke] b-complete square
Dynamic planning idea "from getting started to giving up"
从零开始匹配vim(0)——vimscript 简介
斗地主游戏的案例开发
gnet: 一个轻量级且高性能的 Go 网络框架 使用笔记
Case development of landlord fighting game
C语言实例_2
Add the applet "lazycodeloading": "requiredcomponents" in taro,
mysql: error while loading shared libraries: libtinfo. so. 5: cannot open shared object file: No such
Openjudge noi 1.7 08: character substitution
What are the differences between Oracle Linux and CentOS?
Table table setting fillet
负载均衡性能参数如何测评?
Neon Optimization: performance optimization FAQ QA
[Niuke] [noip2015] jumping stone
移植DAC芯片MCP4725驱动到NUC980
Windows installation mysql8 (5 minutes)
Receive user input, height BMI, BMI detection small business entry case
Installation of gazebo & connection with ROS
Go zero micro service practical series (IX. ultimate optimization of seckill performance)