Compare two long values

int compareTo(Long anotherLong)
Compares two Long objects numerically.
boolean equals(Object obj)
Compares this object to the specified object.

compareTo(Long anotherLong) returns

ValueMeaning
0if this Long is equal to the argument Long;
less than 0if this Long is numerically less than the argument Long;
greater than 0if this Long is numerically greater than the argument Long.

public class Main {
  public static void main(String[] args) {
    Long long1 = new Long(12345L);
    Long long2 = new Long("12346");
    
    System.out.println(long1.compareTo(long2));
  }
}

The output:


-1

If you just want to check the equality,


public class Main {
  public static void main(String[] args) {
    Long long1 = new Long(12345L);
    Long long2 = new Long("12346");
    
    System.out.println(long1.equals(long2));
  }
}

The output:


false
Home 
  Java Book 
    Essential Classes  

Long:
  1. Long class
  2. Constants value from Long class
  3. Constructors from Long
  4. Convert long value to byte, double, float, int, long, short
  5. Decode a string to create long value
  6. Get the long value from a system property
  7. Compare two long values
  8. Parse long value from string
  9. Convert long value to binary, hex and octal format strings
  10. Convert long value to string
  11. Get the sign of the long value
  12. Get the number of zero bits preceding and following
  13. Reverse and rotate a long value