编译时和运行时,存储区域问题

1、编译时就能确定值,因此在方法区中。结果为true

String a = "a1";
String b = "a"+ 1;
System.out.println(a==b); //true

2、编译器不能确定为常量 ,运行期才知道结果、运行时的结果存放在堆中。结果为false

String a = "ab";
String bb = "b";
String b = "a"+bb; //运行时的结果存放在堆中
//编译器不能确定为常量 ,运行期才知道结果
System.out.println(a==b);//false

3、常量,编译器就能确定值。结果为true

String a = "ab";
final String bb = "b"; //常量,编译器
String b = "a"+ bb;
System.out.println(a==b);//true

4、常量的值由运行期决定。结果为false

String a = "ab";
final String bb = getValue(); //运行期才能决定值
String b = "a"+ bb;
System.out.println(a==b);//false

public static String getValue(){
return "b";
}
5、intern()始终返回方法区地址

public class StringBack{
private static String a = new String("ab");
public static void main(String[] args){
String s1 = "a";
String s2 = "b";
String s = s1 + s2;
System.out.println(s == a); //false
System.out.println(s.intern() == a); //false
System.out.println(s.intern() == a.intern());
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值