1.基本数据类型用 ==
long a = (long) 1234567890; long b = (long) 1234567890; if (a == b) { System.out.println("基本数据类型相等"); }
2.包装类使用 equals 或者转换为基本数据类型再用 ==
Long a = (long) 1234567890; Long b = (long) 1234567890; if (a != null && a.equals(b)) { System.out.println("包装类相等"); } if (a != null && a.longValue() == b.longValue()) { System.out.println("包装类相等"); }
3.对象要用 equals
String a1 = null; String b1 = new String();if (a1 != null && a1.length() > 0 && a1.equals(b1)) { System.out.println("对象相等"); }