Long a value equal to determine whether the use of "==", encountered a problem
public class Demo { public static void main(String[] args) { Long m = 1L; Long n = 1L; if (m == n) { System.out.println("m 等于 n"); } else { System.out.println("m 不等于 n"); } } }
Export
m is equal to n
public class Demo { public static void main(String[] args) { Long m = 128L; Long n = 128L; if (m == n) { System.out.println("m 等于 n"); } else { System.out.println("m 不等于 n"); } } }
Export
m is not equal to n
the reason:
If Long value between [-127,128], with "==" is an equality is determined whether no problem
If you do not [-127,128] between, a new object will be new, not use "=="
public static Long valueOf(long l) { final int offset = 128; if (l >= -128 && l <= 127) { // will cache return LongCache.cache[(int)l + offset]; } return new Long(l); }
the solution
(1) use . LongValue ()
public class Demo { public static void main(String[] args) { Long m = 128L; Long n = 128L; if (m.longValue() == n.longValue()) { System.out.println("m 等于 n"); } else { System.out.println("m 不等于 n"); } } }
Export
m is equal to n
(2) using .equals ()
public class Demo { public static void main(String[] args) { Long m = 128L; Long n = 128L; if (m.equals(n)) { System.out.println("m 等于 n"); } else { System.out.println("m 不等于 n"); } } }
Export
m is equal to n
supplement:
The best solution for .eques ()
public boolean equals(Object obj) { if (obj instanceof Long) { return value == ((Long)obj).longValue(); } return false; }