Home » Java Short compareTo() method with Examples

Java Short compareTo() method with Examples

by Online Tutorials Library

Java Short compareTo() Method

The compareTo() method of Java Short class compares two Short Objects. It will be used when we compare two Short values numerically to check these two values are equal or not, less than or greater than.

Syntax

Parameters

X: Another Short value, it will be compared

Return value

  • Return 0, if Short is equal to argument “Short X”.
  • Return greater than 0, if Short is numerically greater than to argument “Short x”.
  • Return less than 0, if Short is numerically less than to argument “Short x”.

Example 1

if Short is equal to argument “Short X”

Test it Now

Output:

when Short s1=45 & s2=45 (both have same Value )  = 0  

Example 2

if Short is numerically greater than to argument “Short x”

Test it Now

Output:

s1=55 & s2=45 (when Short is greater than argument )  = 10  

Example 3

if Short is numerically less than to argument “Short x”

Test it Now

Output:

s1=55 & s2=45 (when Short is less than argument )  = -10 

Next TopicJava Short

You may also like