java 小知识点

1
2
3
4
5
6
public void (){
String s0="helloworld";
String s1="helloworld";

System.out.println(s0==s1);
}
1
2
3
4
5
6
public void test4(){
String s0="helloworld";
String s1=new String("helloworld");

System.out.println( s0==s1 ); //false s0跟s1是不同的对象
}

“==”操作符的作用

  1. 用于基本数据类型的比较
  2. 判断引用是否指向堆内存的同一块地址。
1
2
3
4
5
String s1 = new String("java");
String s2 = new String("java");

System.out.println(s1==s2); //false
System.out.println(s1.equals(s2)); //true 所以字符串比较尽量用equals