String的比较有两种==和equals()方法:
==:比较的是在堆内存中地址
equals():比较的是值
String s1="hello";
String s2="hello";
String s3=new String("hello");
s1==s2;//结果为true
s1==s3;//结果为false
s1.equals(s2);//结果为true
s1.equals(s3);//结果为true
原理图:
String的内容不可改变:
public class StringDemo{
public static void main(String args[]){
String str = "hello" ; // 声明字符串
str = str + " world!!!" ; // 修改字符串
System.out.println("str = " + str) ;
}
};
常用方法: