Tutorials Point


  Java Basics
  Java Object Oriented
  Java Advanced
  Java Useful References
  Java Useful Resources
  Selected Reading

© 2011 TutorialsPoint.COM


  Home     References     Discussion Forums     About TP  

Java - compareTo() Method


previous next AddThis Social Bookmark Button


Description:

The method comapres the Number object that invoked the method to the argument. It is possible to compare Byte,Long,Integer etc.

However two different types cannot be compared, Both the argument and the Number object invoking the method should be of same type.

Syntax:

public int compareTo( NumberSubClass referenceName )

Parameters:

Here is the detail of parameters:

  • referenceName: This could be a Byte, Double, Integer, Float, Long or Short.

Return Value :

  • If the Integer is equal to the argument then 0 is reurned.

  • If the Integer is less than the argument then -1 is returned.

  • If the Integer is greater than the argument then 1 is returned.

Example:

public class Test{ 
   public static void main(String args[]){
      Integer x = 5;
      System.out.println(x.compareTo(3));
      System.out.println(x.compareTo(5));
      System.out.println(x.compareTo(8));            
     }
}

This produces following result:

1
0
-1


previous next Printer Friendly



  

Advertisement

Online Image Processing

Indian Baby Names