当前位置:网站首页>今日问题-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
- Taro 小程序开启wxml代码压缩
- Using the entry level of DVA in taro3.*
- Lldp compatible CDP function configuration
- SuperSocket 1.6 创建一个简易的报文长度在头部的Socket服务器
- THREE.AxesHelper is not a constructor
- Meet in the middle
- 树莓派/arm设备上安装火狐Firefox浏览器
- Vocabulary in Data Book
- Can the system hibernation file be deleted? How to delete the system hibernation file
猜你喜欢

Installation of gazebo & connection with ROS

Windows installation mysql8 (5 minutes)

Gazebo的安装&与ROS的连接

字节P7专业级讲解:接口测试常用工具及测试方法,福利文

Wood extraction in Halcon

go-zero微服务实战系列(九、极致优化秒杀性能)

【信号与系统】

1123. 最深叶节点的最近公共祖先

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

JTAG debugging experience of arm bare board debugging
随机推荐
The cost of returning tables in MySQL
[case sharing] basic function configuration of network loop detection
What are the differences between Oracle Linux and CentOS?
docker 方法安装mysql
Atomic in golang, and cas Operations
Taro2.* 小程序配置分享微信朋友圈
Atomic in golang and CAS operations
AI 从代码中自动生成注释文档
"Exquisite store manager" youth entrepreneurship incubation camp - the first phase of Shunde market has been successfully completed!
ClickHouse字段分组聚合、按照任意时间段粒度查询SQL
Can the system hibernation file be deleted? How to delete the system hibernation file
1123. 最深叶节点的最近公共祖先
Oracle: Practice of CDB restricting PDB resources
Neon Optimization: an instruction optimization case of matrix transpose
Openjudge noi 1.7 08: character substitution
c语言—数组
力扣1037. 有效的回旋镖
NEON优化:性能优化常见问题QA
Vocabulary in Data Book
Force buckle 1037 Effective boomerang