面试题:
1.空串与NUll的 区别
空串:“”;空字符串,有指向对象
null:没有指向任何一个对象,
String s=null;
s.length();
NullPointException
2.字符串一旦初始化就不可以被改变
String s="hello";
s+="World";
System.out.println(s); //helloWord
注意:这里指的是字符串在常量池里面的值不能发生改变,
而不是说字符串的引用不能变
3.String s1=new String("abc");
String s2="abc";
第一种情况在内存中有两个对象,堆内存new String();常量池"abc”
而第二种在内存中常量池只有一个对象
4.String s1="";
String s2=null;
System.out.println(s1.isEmpty()); //ture
System.out.println(s2.isEmpty()); //nullPointException异常
|
|