当前位置:网站首页>今日问题-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 **/
边栏推荐
- Force buckle 1037 Effective boomerang
- LeetCode:1175. 质数排列
- How to evaluate load balancing performance parameters?
- Maidong Internet won the bid of Beijing life insurance to boost customers' brand value
- mysql: error while loading shared libraries: libtinfo.so.5: cannot open shared object file: No such
- HMM notes
- 安全保护能力是什么意思?等保不同级别保护能力分别是怎样?
- AI 从代码中自动生成注释文档
- NEON优化:log10函数的优化案例
- c语言—数组
猜你喜欢
[hfctf2020]babyupload session parsing engine
Boot - Prometheus push gateway use
[signal and system]
"Exquisite store manager" youth entrepreneurship incubation camp - the first phase of Shunde market has been successfully completed!
移植DAC芯片MCP4725驱动到NUC980
[100 cases of JVM tuning practice] 05 - Method area tuning practice (Part 2)
让我们,从头到尾,通透网络I/O模型
动态规划思想《从入门到放弃》
Tensorflow GPU installation
Make Jar, Not War
随机推荐
[case sharing] basic function configuration of network loop detection
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
黑马笔记---创建不可变集合与Stream流
Meet in the middle
Lldp compatible CDP function configuration
Can the system hibernation file be deleted? How to delete the system hibernation file
Neon Optimization: an instruction optimization case of matrix transpose
Typical problems of subnet division and super network construction
黑马笔记---异常处理
Sword finger offer II 035 Minimum time difference - quick sort plus data conversion
Transformation transformation operator
[Niuke] b-complete square
Case development of landlord fighting game
Supersocket 1.6 creates a simple socket server with message length in the header
ESP Arduino (IV) PWM waveform control output
Gazebo的安装&与ROS的连接
docker 方法安装mysql
C语言实例_5
ARM裸板调试之JTAG调试体验
ARM裸板调试之JTAG原理