当前位置:网站首页>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 **/
边栏推荐
- 让我们,从头到尾,通透网络I/O模型
- Spark TPCDS Data Gen
- Taro applet enables wxml code compression
- Supersocket 1.6 creates a simple socket server with message length in the header
- 编译命令行终端 swift
- 剑指 Offer II 035. 最小时间差-快速排序加数据转换
- Receive user input, height BMI, BMI detection small business entry case
- THREE. AxesHelper is not a constructor
- 从底层结构开始学习FPGA----FIFO IP的定制与测试
- Send template message via wechat official account
猜你喜欢

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

tansig和logsig的差异,为什么BP喜欢用tansig

免费白嫖的图床对比
![JS reverse -- ob confusion and accelerated music that poked the [hornet's nest]](/img/40/da56fe6468da64dd37d6b5b0082206.png)
JS reverse -- ob confusion and accelerated music that poked the [hornet's nest]

1123. 最深叶节点的最近公共祖先
![[case sharing] basic function configuration of network loop detection](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[case sharing] basic function configuration of network loop detection

Dark horse notes - create immutable sets and streams

2022 Google CTF SEGFAULT LABYRINTH wp

Byte P7 professional level explanation: common tools and test methods for interface testing, Freeman

让我们,从头到尾,通透网络I/O模型
随机推荐
mysqlbackup 还原特定的表
736. LISP syntax parsing: DFS simulation questions
Spark TPCDS Data Gen
The cost of returning tables in MySQL
C # method of calculating lunar calendar date 2022
Start from the bottom structure to learn the customization and testing of fpga---- FIFO IP
NEON优化:矩阵转置的指令优化案例
C language instance_ four
shell脚本快速统计项目代码行数
Wood extraction in Halcon
Match VIM from zero (0) -- Introduction to vimscript
uva 1401 dp+Trie
Taro2.* applet configuration sharing wechat circle of friends
AI automatically generates annotation documents from code
json学习初体验–第三者jar包实现bean、List、map创json格式
Let's see through the network i/o model from beginning to end
Sword finger offer II 035 Minimum time difference - quick sort plus data conversion
Implementation principle of waitgroup in golang
C语言实例_4
C语言实例_5