String相加解析

 

Java代码

package com.utils.test;

public class TestObject {


    public static void main(String[] args) {

          String a = "hello2";
    final String b = "hello";
          String d = "hello";
          String c = b + 2;
          String e = d + 2;
          String f ="hello"+"2";
        System.out.println(a == b); //false
        System.out.println(a == c); //true
        System.out.println(a == e); //false
        System.out.println(a == f); //true
        System.out.println(System.identityHashCode(a));
        System.out.println(System.identityHashCode(b));
        System.out.println(System.identityHashCode(c));
        System.out.println(System.identityHashCode(d));
        System.out.println(System.identityHashCode(e));
        System.out.println(System.identityHashCode(f));

    }
}

首先要明确的是 引用数据类型 == 比较的是地址值,equal 没重写比较的是地址值,重写之后比较的是内容.String重写了,StringBuffer没有重写

其次:

a==c 为true是因为 b为final类型,当final变量是基本数据类型以及String类型时,如果在编译期间能知道它的确切值,则编译器会把它当做编译期常量使用。也就是说在用到该final变量的地方,相当于直接访问的这个常量,不需要在运行时确定。因此在上面的一段代码中,由于变量b被final修饰,因此会被当做编译器常量,所以在使用到b的地方会直接将变量b 替换为它的  值。所以 c = b+2 等于 c = “hello”+“2” 同e

a ==e 为false是因为e = d+2 其实是创建了一个StringBuffer对象,然后用StringBuffer对象执行append方法来创建出字符串对象“ab”,然后再转换成为String。但是这个转换后的String对象,也就是上面的s3是放在堆里面的。而s4则是字符串常量,放在常量池里面。所以返回的是false。 

a ==f 为true 是因为: 常量相加的时候,其实是在编译的时候就直接相加为“hello2”,这是JVM的优化,所以运行的时候,a和f的字节码是一样的。因为在常量池中有了一个“hello2”,所以两个引用指向的是一个字符串“hello2”,所以返回结果也是true。

参考出处:https://blog.csdn.net/qq_23367963/article/details/81937325 

猜你喜欢

转载自www.cnblogs.com/coloz/p/10917386.html