티스토리 뷰

JAVA

compareTo method in Java

seoca 2019. 2. 21. 10:49



compareTo() method in Java 


compareTo() method compares the Number and an argument. The argument and the number object should be the same types. 




Example code


public class Test { 
   public static void main(String args[]) {
      Integer i = 3;
      
      System.out.println(i.compareTo(2)); //returns '1' : Integer is greater than the argument
      System.out.println(i.compareTo(3)); //returns '0' : Integer is equal to the argument
      System.out.println(i.compareTo(8)); //returns '-1' : Integer is less than the argument           
   }
}






Reference 

https://www.tutorialspoint.com/java/number_compareto.htm

'JAVA' 카테고리의 다른 글

How to convert Arrays to List  (0) 2019.02.26
Lambda Expression in Java  (0) 2019.02.22
Reference Data Type in Java  (0) 2019.02.20
Anonymous class in Java  (0) 2019.02.20
What's the difference between two different List declaration?  (0) 2019.02.02