Short compareTo() method in Java with Examples Last Updated : 05 Dec, 2018 Summarize Comments Improve Suggest changes Share Like Article Like Report The compareTo() method of Short class is a built in method in Java which is used to compare twoShort objects numerically. Syntax: public int compareTo(Short otherShort) Parameters : This method accepts a mandatory parameter otherShort which is the Short object to be compared. Return type : It returns an int value. It returns: 0 if 'otherShort' is equal to 'thisShort', a positive value 'thisShort' is lesser than 'otherShort', a negative value 'otherShort' is greater than 'thisShort' Below is the implementation of compareTo() method in Java: Example 1: Java // Java code to demonstrate // Short compareTo() method class GFG { public static void main(String[] args) { // creating a Short object Short a = new Short("4"); // creating a Short object Short b = new Short("4"); // compareTo method in Short class int output = a.compareTo(b); // printing the output System.out.println("Comparing " + a + " and " + b + " : " + output); } } Output: Comparing 4 and 4 : 0 Example 2: Java // Java code to demonstrate // Short compareTo() method class GFG { public static void main(String[] args) { // creating a Short object Short a = new Short("4"); // creating a Short object Short b = new Short("2"); // compareTo method in Short class int output = a.compareTo(b); // printing the output System.out.println("Comparing " + a + " and " + b + " : " + output); } } Output: Comparing 4 and 2 : 2 Example 3: Java // Java code to demonstrate // Short compareTo() method class GFG { public static void main(String[] args) { // creating a Short object Short a = new Short("2"); // creating a Short object Short b = new Short("4"); // compareTo method in Short class int output = a.compareTo(b); // printing the output System.out.println("Comparing " + a + " and " + b + " : " + output); } } Output: Comparing 2 and 4 : -2 Comment More infoAdvertise with us Next Article Short compareTo() method in Java with Examples S Sruti Rai Follow Improve Article Tags : Misc Java java-basics Java-lang package Java-Functions Java-Short +2 More Practice Tags : JavaMisc Similar Reads Java String compareTo() Method with Examples Strings in Java are objects that are supported internally by an array only which means contiguous allocation of memory for characters. Please note that strings are immutable in Java which means once we create a String object and assign some values to it, we cannot change the content. However, we can 7 min read Byte compareTo() method in Java with examples The compareTo() method of Byte class is a built in method in Java which is used to compare the given Byte type object with the instance of Byte invoking the compareTo() method. Syntax ByteObject.compareTo(Byte a) Parameters: It takes a Byte type object a as input which is to be compared with the ins 2 min read Year compareTo() method in Java with Examples The compareTo() method of Year class in Java is used to compare this Year object with another Year object. The comparison of Year object is based on the values of Year.Syntax: public int compareTo(Year otherYear) Parameter: This method accepts a single parameter otherYear. It is the Year object whic 2 min read Float compareTo() method in Java with examples The comapreTo() method of Float Class is a built-in method in Java that compares the two specified float values. The sign of the integer value returned is the same as that of the integer that would be returned by the function call. Syntax: public int compareTo(Object f) Parameters: The function acce 2 min read Double.compareTo() Method in Java with Examples The Double.compareTo() method is a built-in method in Java of java.lang package. This method is used to compare two Double objects numerically. This method returns 0 if this object is equal to the argument object, it returns less than 0 if this object is numerically less than the argument object, an 3 min read Like