public class Test {
    public static void main(String[] args) {
        Integer i=555,j=555;
        System.out.println(i==j); //false
        Integer l=5,n=5;
        System.out.println(l==n); //true
    }
}
なぜ、ジャバ?それはどのように可能ですか?
Integer同じ値を持つ 2 つの異なるクラス インスタンスの参照を比較しているため、equalsメソッドを使用する必要があります (オブジェクト間の等価性を比較する必要があるため)。
Integer i=555,j=555;
System.out.println(i==j); //false
Integer i=555,j=555;
System.out.println(i.equals(j)); //true
ただし、-128 ~ 127 の値のオブジェクト インスタンスIntegerのプールがあります。Integerint
Integer l=5,n=5;
System.out.println(l==n); //true
truesincelを受け取り、同じオブジェクト参照をn 指しています。