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出来的堆中的地址
![在这里插入图片描述](https://img-blog.csdnimg.cn/20190527174436640.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzE3NzgyMzg5,size_16,color_FFFFFF,t_70)
 public class StringDemo1 {
        public static void main(String[] args) {
            String s1 = new String("hello");
            String s2 = new String("hello");
            System.out.println(s1 == s2);// false
            System.out.println(s1.equals(s2));// true
    
            String s3 = new String("hello");
            String s4 = "hello";
            System.out.println(s3 == s4);// false
            System.out.println(s3.equals(s4));// true
    
            String s5 = "hello";
            String s6 = "hello";
            System.out.println(s5 == s6);// true
            System.out.println(s5.equals(s6));// true
        }
    }

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

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

public class StringDemo2 {
    public static void main(String[] args) {
        String s1 = "hello";
        String s2 = "world";
        String s3 = "helloworld";
        System.out.println(s3 == s1 + s2);// false
        System.out.println(s3.equals((s1 + s2)));// true
        System.out.println(s3 == "hello" + "world");//false
        System.out.println(s3.equals("hello" + "world"));// true
    }
}

Demo2详解

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、字符串如果是常量相加,是先加,然后在常量池找,如果有就直接返回,否则,就创建。

你可能感兴趣的:(java,面试题)