为什么 String s1="hello" String s2 = new String("hello") s1==s2 为flase

    String s2 = new String("hello")  创建三个对象  若常量池中没有hello 创建一个 hello 对象 再在堆中创建一个new String 对象 还有 一个栈中的 数据类型为String s2的变量

    s2所保存的地址是堆中创建对象地址

     String s1="hello" 查看字符串常量池中有无hello对象 无则创建对象 返回地址值 若有 则直接返回地址值

猜你喜欢

转载自www.cnblogs.com/zhongmeilin/p/11616399.html