關於Java Integer和Long直接比較


Integer和Long不能直接equals比較會返回False

Long.class源碼

`

public boolean equals(Object obj) {
    if (obj instanceof Long) {
        return this.value == (Long)obj;
    } else {
        return false;
    }
}

`

Integer.class源碼

`

public boolean equals(Object obj) {
    if (obj instanceof Integer) {
        return this.value == (Integer)obj;
    } else {
        return false;
    }
}

`

解決方法

Long變量.equals(Integer變量.longValue()) 返回True


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM