java issue of equal

!= >= <=

  • Respectively not equal, greater than or equal, less than equal (by size determination of the numeric types)

== && equals

  • == two variables are determined or examples are not directed to the same memory space, equals the value of two variables are determined or examples points to the memory space is not the same
  • == refers to the memory address are compared, equals () is compared to the contents of the string
  • == is the same references refer, equals () refers to the values ​​are identical

compareTo

  • compareTo (Object o) method is a method java.lang.Comparable interface, when the objects need to be sorted in a class, the class need to implement Comparable interface, must override public int compareTo (T o) method.
  • It will be realized that the force of each of the object class called total ordering ----- natural ordering of the class, and to achieve this object list may be the Collections.sort array interface (), and Arrays.sort () were automatic sorting;
  • In other words, as long as the object implements this interface (array) equivalent capacity has been ordered, so called comparable- sortable, so you can say that this is an internal sort of way, the only way to achieve it by compareTo ().

compare

  • compare (Object o1, Object o2) java.util.Comparator interface method is a method, it is actually in the compareTo (Object o) method of the object to be compared.
  • For it is not the object itself against some of the comparative ability (array) to achieve more for their function, so it is called a comparator, is an external thing, it is defined by way of comparison, then spread Collection.sort () and Arrays.sort () to sort the target, and by its own method compare () defines the content and descending liter of the comparison result;
Published 53 original articles · won praise 13 · views 2266

Guess you like

Origin blog.csdn.net/qq_36821220/article/details/103296647