java相等_Java 判断相等
1.除 float 和 double 外的原始數據類型 : 使用 ==
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= newString();if (a1 != null && a1.length() > 0 &&a1.equals(b1)) {
System.out.println("對象相等");
}
附錄:
public static void main(String[] args) {
System.out.println("-----");
int a = 200;
int a2 = 200;
if (a == a2) {
System.out.println("相等");//√
}
System.out.println("-----");
Integer b = 200;
Integer b2 = 200;
if (b == b2) {
System.out.println("相等");//-128~127√
}
if (b.equals(b2)) {
System.out.println("相等2");//√
}
System.out.println("-----");
Integer c = new Integer(200);
Integer c2 = new Integer(200);
if (c == c2) {
System.out.println("相等");
}
if (c.equals(c2)) {
System.out.println("相等2");//√
}
System.out.println("-----");
if (a==c){
System.out.println("相等");//√
}
if (c.equals(a)){
System.out.println("相等2");//√
}
if (b==c){
System.out.println("相等3");
}
if (c.equals(b)){
System.out.println("相等4");//√
}
}
public static void main(String[] args) {
System.out.println("-----");
String a = "a";
String a2 = "a";
if (a == a2) {
System.out.println("相等");//√
}
if (a.equals(a2)) {
System.out.println("相等2");//√
}
System.out.println("-----");
String b = new String("a");
String b2 = new String("a");
if (b == b2) {
System.out.println("相等");
}
if (b.equals(b2)) {
System.out.println("相等2");//√
}
System.out.println("-----");
if (a == b) {
System.out.println("相等");
}
if (a.equals(b)) {
System.out.println("相等2");//√
}
}
public static void main(String[] args) {
System.out.println("-----");
boolean a = true;
boolean a2 = true;
if (a == a2) {
System.out.println("相等");//√
}
System.out.println("-----");
Boolean b = true;
Boolean b2 = true;
if (b == b2) {
System.out.println("相等");//√
}
if (b.equals(b2)) {
System.out.println("相等2");//√
}
System.out.println("-----");
Boolean c = new Boolean(true);
Boolean c2 = new Boolean(true);
if (c == c2) {
System.out.println("相等");
}
if (c.equals(c2)) {
System.out.println("相等2");//√
}
System.out.println("-----");
if (a == c) {
System.out.println("相等");//√
}
if (c.equals(a)) {
System.out.println("相等2");//√
}
if (b == c) {
System.out.println("相等3");
}
if (c.equals(b)) {
System.out.println("相等4");//√
}
}
超類 Object 中有這個 equals() 方法,該方法主要用于比較兩個對象是否相等。該方法的源碼如下:
public boolean equals(Object obj) {
return (this == obj);
}
我們知道所有的對象都擁有標識(內存地址)和狀態(數據),同時“==”比較兩個對象的的內存地址,所以說使用 Object 的 equals() 方法是比較兩個對象的內存地址是否相等,即若 object1.equals(object2) 為 true,則表示 equals1 和 equals2 實際上是引用同一個對象。雖然有時候 Object 的 equals() 方法可以滿足我們一些基本的要求,但是我們必須要清楚我們很大部分時間都是進行兩個對象的比較,這個時候 Object 的 equals() 方法就不可以了,實際上 JDK 中,String、Math 等封裝類都對 equals() 方法進行了重寫。
在 Java 規范中,它對 equals() 方法的使用必須要遵循如下幾個規則:
equals 方法在非空對象引用上實現相等關系:
1、自反性:對于任何非空引用值 x,x.equals(x) 都應返回 true。
2、對稱性:對于任何非空引用值 x 和 y,當且僅當 y.equals(x) 返回 true 時,x.equals(y) 才應返回 true。
3、傳遞性:對于任何非空引用值 x、y 和 z,如果 x.equals(y) 返回 true,并且 y.equals(z) 返回 true,那么 x.equals(z) 應返回 true。
4、一致性:對于任何非空引用值 x 和 y,多次調用 x.equals(y) 始終返回 true 或始終返回 false,前提是對象上 equals 比較中所用的信息沒有被修改。
5、對于任何非空引用值 x,x.equals(null) 都應返回 false。
對于上面幾個規則,我們在使用的過程中最好遵守,否則會出現意想不到的錯誤。
在 java 中進行比較,我們需要根據比較的類型來選擇合適的比較方式:
1)?對象域,使用 equals 方法 。
2)?類型安全的枚舉,使用 equals 或== 。
3)?可能為 null 的對象域 : 使用 == 和 equals 。
4)?數組域 : 使用 Arrays.equals 。
5)除 float 和 double 外的原始數據類型 : 使用 == 。
6)?float 類型: 使用 Float.foatToIntBits 轉換成 int 類型,然后使用==。
7)?double 類型: 使用 Double.doubleToLongBit 轉換成 long 類型,然后使用==。
總結
以上是生活随笔為你收集整理的java相等_Java 判断相等的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 添加javascript代码:_Java
- 下一篇: java美元兑换,(Java实现) 美元