当前位置:网站首页>今日问题-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 **/
边栏推荐
- 1123. 最深叶节点的最近公共祖先
- 树莓派/arm设备上安装火狐Firefox浏览器
- 接收用户输入,身高BMI体重指数检测小业务入门案例
- UI control telerik UI for WinForms new theme - vs2022 heuristic theme
- Gnet: notes on the use of a lightweight and high-performance go network framework
- NEON优化:关于交叉存取与反向交叉存取
- There is an error in the paddehub application
- Dynamic planning idea "from getting started to giving up"
- 线段树(SegmentTree)
- Go zero micro service practical series (IX. ultimate optimization of seckill performance)
猜你喜欢

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

pytorch之数据类型tensor

Gazebo的安装&与ROS的连接

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

ClickHouse字段分组聚合、按照任意时间段粒度查询SQL
![[100 cases of JVM tuning practice] 05 - Method area tuning practice (Part 2)](/img/40/dc45df3cd3ee7642277eff899bc6aa.png)
[100 cases of JVM tuning practice] 05 - Method area tuning practice (Part 2)

UI控件Telerik UI for WinForms新主题——VS2022启发式主题

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

Maidong Internet won the bid of Beijing life insurance to boost customers' brand value

HMM 笔记
随机推荐
The MySQL database in Alibaba cloud was attacked, and finally the data was found
C# 计算农历日期方法 2022
C # method of calculating lunar calendar date 2022
Spark TPCDS Data Gen
Boot - Prometheus push gateway use
自旋与sleep的区别
阿里云中mysql数据库被攻击了,最终数据找回来了
golang中的atomic,以及CAS操作
数据手册中的词汇
Atomic in golang and CAS operations
Install Firefox browser on raspberry pie /arm device
子网划分、构造超网 典型题
Implementation principle of waitgroup in golang
资产安全问题或制约加密行业发展 风控+合规成为平台破局关键
【信号与系统】
mysql: error while loading shared libraries: libtinfo. so. 5: cannot open shared object file: No such
界面控件DevExpress WinForms皮肤编辑器的这个补丁,你了解了吗?
移植DAC芯片MCP4725驱动到NUC980
pytorch之数据类型tensor
Tensorflow 1.14 specify GPU running settings