equals 和 == 的区别

*

* @author FlyFive(pengfei.dongpf@gmail.com)

* created on 2013-1-5

*/

public class EqualsTest {

public static void main(String[] args) {

String s01 = new String("hello world");

String s02 = new String("hello world");

System.out.println("两个new出来的String");

System.out.println(s01.equals(s02));

System.out.println(s01 == s02);

String s11 = new String("hello world");

String s12 = s11;

System.out.println("两个相同的String");

System.out.println(s11 == s12);

System.out.println(s11 == s12);

String s21 = "hello world";

String s22 = "hello world";

System.out.println("两个直接赋值的String");

System.out.println(s21.equals(s21));

System.out.println(s21 == s22);

Object s31 = new Object();

Object s32 = new Object();

System.out.println("两个new出来的普通对象");

System.out.println(s31.equals(s31));

System.out.println(s31 == s32);

Integer s41 = new Integer(1);

Integer s42 = new Integer(1);

System.out.println("两个new出来的基本类型包装类");

System.out.println(s41.equals(s41));

System.out.println(s41 == s42);

}

}

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐