当前位置:网站首页>Today's question -2022/7/4 modify string reference type variables in lambda body
Today's question -2022/7/4 modify string reference type variables in lambda body
2022-07-07 01:30:00 【Witty little yuan】
explain :
1:lambda In the scope of the expression , There are no restrictions on local reference variables . That is to say, it can be in lambda Body to modify the value of the local reference variable . The reason is that the value of the reference variable is stored in the heap , It is shared by threads, so Lambda You can modify his value .
Have a problem
String Variables of type are reference variables, right , however String The variables of type are lambda Body cannot be modified .
public class Test {
static String staticStr = " Static variables ";
String instanceStr = " Instance variables ";
public static void main(String[] args) {
/** * There's a problem ,String Not a reference type ? Why is it lambda Revision in China String Type variable failed . */
int t = 0;
List<String> list = new ArrayList<>();
list.add(" I am the first 0 Elements ");
String str = " I am a local reference variable ";
WorkerInterface workerInterface = (a,b)->{
System.out.println("a:"+a+"b:"+b);
list.get(0);
list.add("eee");
// An error will be reported here
System.out.println(str);
int c = t+1;
System.out.println("c:"+c);
};
workerInterface.doSomeWork(3,4);
list.add("33");
str = str+" I changed ";
}
}
/** * Custom functional interface * @author wangdawei */
@FunctionalInterface
public interface WorkerInterface {
/** * An abstract method */
public void doSomeWork(int a,int b);
}
guess
It may be in String Type optimization ,String Types maintain a string constant pool , Strings in the pool will be reused , Strings in the pool are public to threads .
solve
stay lambda It is allowed to modify the data of local reference type variables , However, it is not allowed to modify the reference point . Again because String Types are immutable types , That is, if you want to modify String The value of the type variable , That is to point the current reference to another address . That is to change the reference point , So it's wrong .
Answer
If you want to use it, you can use a variable string
public class Test {
static String staticStr = " Static variables ";
String instanceStr = " Instance variables ";
public static void main(String[] args) {
/** * There's a problem ,String Not a reference type ? Why is it lambda Revision in China String Type variable failed . */
int t = 0;
List<String> list = new ArrayList<>();
list.add(" I am the first 0 Elements ");
String str = " I am a local reference variable ";
WorkerInterface workerInterface = (a,b)->{
System.out.println("a:"+a+"b:"+b);
list.get(0);
list.add("eee");
stringBuffer.append(" I have changed ");
System.out.println(stringBuffer.toString());
stringBuilder.append(" I have changed ");
System.out.println(stringBuilder.toString());
int c = t+1;
System.out.println("c:"+c);
};
workerInterface.doSomeWork(3,4);
list.add("33");
}
}
/** * result : * I am a variable string StringBuffer I have changed * I am a variable string stringBuilder I have changed *c:1 **/
边栏推荐
- Js逆向——捅了【马蜂窝】的ob混淆与加速乐
- MySQL script batch queries all tables containing specified field types in the database
- C language instance_ two
- Byte P7 professional level explanation: common tools and test methods for interface testing, Freeman
- Neon Optimization: an optimization case of log10 function
- Taro2.* 小程序配置分享微信朋友圈
- Spark TPCDS Data Gen
- 移植DAC芯片MCP4725驱动到NUC980
- 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
猜你喜欢
Transformation transformation operator
Boot - Prometheus push gateway use
MySQL script batch queries all tables containing specified field types in the database
Yunna - work order management system and process, work order management specification
免费白嫖的图床对比
子网划分、构造超网 典型题
HMM 笔记
AI automatically generates annotation documents from code
Js逆向——捅了【马蜂窝】的ob混淆与加速乐
[case sharing] basic function configuration of network loop detection
随机推荐
Gnet: notes on the use of a lightweight and high-performance go network framework
Typical problems of subnet division and super network construction
机器学习:随机梯度下降(SGD)与梯度下降(GD)的区别与代码实现。
Metauniverse urban legend 02: metaphor of the number one player
公钥\私人 ssh避password登陆
Your cache folder contains root-owned files, due to a bug in npm ERR! previous versions of npm which
Neon Optimization: an instruction optimization case of matrix transpose
Let's see through the network i/o model from beginning to end
Machine learning: the difference between random gradient descent (SGD) and gradient descent (GD) and code implementation.
搭建【Redis in CentOS7.x】
Wood extraction in Halcon
Instructions for using the domain analysis tool bloodhound
Sword finger offer II 035 Minimum time difference - quick sort plus data conversion
C# 计算农历日期方法 2022
剑指 Offer II 035. 最小时间差-快速排序加数据转换
LeetCode:1175. Prime permutation
增加 pdf 标题浮窗
Taro applet enables wxml code compression
C language instance_ three
让我们,从头到尾,通透网络I/O模型