JAVA字符串池(String pool)的困惑题目

废话不多说,先放图。
简单题目
在Java中有两种创建字符串对象的方式:
1)采用字面值的方式赋值
2)采用new关键字新建一个字符串对象。这两种方式在性能和内存占用方面存在着差别。
public class Test {
/*字符串一旦创建,不能发生改变长度。
*/
public static void main(String[] args) {
String s1 = “Hello World” ;
//在编译时运行
String s2 = "Hello " + “World” ;
String temp1 = “Hello " ;
String temp2 = “World” ;
//两个变量相加在运行时相加运算
String s5 = temp1 + temp2 ;
String s6=(temp1+temp2).intern();
//符号”"是比较内存地址
boolean flag1 = s1 s2 ;
boolean flag2 = s1
s5 ;
boolean flag3=s1
s6;
System.out.println(“flag1:”+flag1);
System.out.println(“flag2:”+flag2);
System.out.println(“flag3:”+flag3);
}
}
详情参考https://www.cnblogs.com/cold-windy/p/11514977.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值