zoukankan      html  css  js  c++  java
  • String str=new String("a")和String str = "a"有什么区别?

    面试官问

    String A=“ABC”;
    String B=new String(“ABC”);
    这两个值,A,B 是否相等,如果都往HashSet里面放,能放下吗?

    答:
    (a)A==B 的判断为false;
    (b)A.equals(B)为true ;因为值相等,所以都往HashSet里面放不下,只能放一个

    String A = "ABC";内存会去查找永久代(常量池) ,如果没有的话,在永久代中中开辟一块儿内存空间,把地址付给栈指针,如果已经有了"ABC"的内存,直接把地址赋给栈指针; 因此 
    String str1="aa";
    Srting str2="aa";
    String Str3="aa";
    ....
    这样下去,str1==Str2==str3;会一直相等下去,(a) ==的判断, (b) equals()的判断;都相等,因为他们的地址都相等,因此只在常量池中有一份内存空间,地址全部相同; 
    而String str = new String("a");是根据"a"这个String对象再次构造一个String对象;在堆中从新new一块儿内存,把指针赋给栈, 将新构造出来的String对象的引用赋给str。 因此 只要是new String(),则,栈中的地址都是指向最新的new出来的堆中的地址 

     1 public class Test1 {
     2     public static void main(String[] args) {
     3         String s1 = new String("hello");
     4         String s2 = new String("hello");
     5         System.out.println(s1 == s2);// false
     6         System.out.println(s1.equals(s2));// true
     7 
     8         String s3 = new String("hello");
     9         String s4 = "hello";
    10         System.out.println(s3 == s4);// false
    11         System.out.println(s3.equals(s4));// true
    12 
    13         String s5 = "hello";
    14         String s6 = "hello";
    15         System.out.println(s5 == s6);// true
    16         System.out.println(s5.equals(s6));// true
    17     }
    18 
    19 }

    Test1详解
    s1~s6用equals()的比较不解释,都是比较的值,均为true。以下讲解==

    s1、s2:二者均为new出来的,各自在堆中分配有空间,并各自将内存地址赋值给s1、s2。空间地址不同,==比较为false。但是各自在堆中空间中保存的值均为在字符串常量池中的同一个对象的地址。根据Demo处的图即解释不难理解。
    s3、s4同上Demo出解释。
    s5、s6都是在常量池中取值,二者都指向常量池中同一对象,其地址值相同,所以结果为true

     1 public class Test2 {
     2     public static void main(String[] args) {
     3          String s1 = "hello";
     4             String s2 = "world";
     5             String s3 = "helloworld";
     6             System.out.println(s3 == s1 + s2);// false
     7             System.out.println(s3.equals((s1 + s2)));// true
     8             System.out.println(s3 == "hello" + "world");//true
     9             System.out.println(s3.equals("hello" + "world"));// true
    10     }
    11 }

    Test2详解

    equals()比较方法不解释,比较值,均相等,均为true。

    s1与s2相加是先在字符串常量池中开一个空间,然后拼接,这个空间的地址就是s1与s2拼接后的地址。与s3的地址不同,所以输出为false。
    s3与”hello”+”world”作比较,”hello”+”world”先拼接成”helloworld”,然后再去字符串常量池中找是否有”helloworld”,有,所以和s3共用一个字符串对象,则为true。

    总结
    1、String s = new String(“hello”)会创建2(1)个对象,String s = “hello”创建1(0)个对象。
    注:当字符串常量池中有对象hello时括号内成立!
    2、字符串如果是变量相加,先开空间,在拼接。
    3、字符串如果是常量相加,是先加,然后在常量池找,如果有就直接返回,否则,就创建。

  • 相关阅读:
    LOJ 6089 小Y的背包计数问题 —— 前缀和优化DP
    洛谷 P1969 积木大赛 —— 水题
    洛谷 P1965 转圈游戏 —— 快速幂
    洛谷 P1970 花匠 —— DP
    洛谷 P1966 火柴排队 —— 思路
    51Nod 1450 闯关游戏 —— 期望DP
    洛谷 P2312 & bzoj 3751 解方程 —— 取模
    洛谷 P1351 联合权值 —— 树形DP
    NOIP2007 树网的核
    平面最近点对(加强版)
  • 原文地址:https://www.cnblogs.com/linliquan/p/11259483.html
Copyright © 2011-2022 走看看