关于 final,堆栈,==和equals,加上值传递和引用传递的疑惑和解答

直接上图:

猜测:为什么a==c

1.因为b是final修饰,所以b属于最终变量,此处可以理解为常量,因为String采用的是和八种基本类型一直的传参方式,即值传递,又被final修饰称为常量,所以b在使用的时候,采用的是直接替换,a中的"hello"就是b。a相当于b+“2”;变向输出内存地址的方法:System.identityHashCode(a)

参考代码:

public static void main(String[] args) {
    String a = "hello2";
    final String b = "hello";
    final String bb = "2";
    String d = "hello";
    String c = b + 2;
    String e = d + 2;
    String ccc = b + "2";
    String cc = "hello" + bb;
    String ee = "hello" + "2";
    String ff = b + bb;
    System.out.println(ee == ff);
    System.out.println(System.identityHashCode(a));
    System.out.println(System.identityHashCode(ccc));
    System.out.println(System.identityHashCode(c));
    System.out.println(System.identityHashCode(e));
    System.out.println(System.identityHashCode(b));
    System.out.println(System.identityHashCode(2));
    System.out.println(System.identityHashCode("2"));
    System.out.println(System.identityHashCode("hello"));
    System.out.println((a == c));
    System.out.println(a.getClass().hashCode());
    System.out.println(c.getClass().hashCode());
    System.out.println(e.getClass().hashCode());
    System.out.println((a == cc));
    System.out.println(a.equals(c));
    System.out.println(a.equals(e));
    System.out.println((a == e));
    System.out.println(c);
    System.out.println(e);
}

原版出处:http://www.cnblogs.com/dolphin0520/p/3736238.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值