字符串的特点:一旦被赋值,就不能被改变
String s = “hello”; s += “world”; 问s的结果是多少?
public class StringDemo2 {
public static void main(String[] args) {
String s = "hello";
s += "world";
System.out.println("s:" + s);//
}
}
String s = new String(“hello”)和String s = “hello”;的区别?
1、==比较引用数据类型比较的是地址值
2、equals默认比较的是地址值,但是由于String类中重写了该方法,所以比较的是内容
3、String s = new String(“hello”) 会在堆内存中创建对象
public class StringDemo3 {
public static void main(String[] args) {
String s1 = new String("hello");
String s2 = "hello";
System.out.println(s1 == s2); // false
System.out.println(s1.equals(s2)); //true
}
}