Java.lang.Short.compareTo() 方法
描述
java.lang.Short.compareTo() 方法对两个 Short 对象进行数值比较。
声明
以下是 java.lang.Short.compareTo() 方法的声明。
public int compareTo(Short anotherShort)
参数
anotherShort − 这是要比较的 Short。
返回值
如果此 Short 等于参数 Short,则此方法返回值 0,如果此 Short 在数值上小于参数 Short,则返回小于 0 的值; 如果此 Short 在数值上大于参数 Short,则值大于 0。
异常
NA
示例
下面的例子展示了 java.lang.Short.compareTo() 方法的使用。
package com.tutorialspoint; import java.lang.*; public class ShortDemo { public static void main(String[] args) { // create short object and assign value to it short val1 = 50, val2 = 200, val3 = 50; Short Shortval1 = new Short(val1); Short Shortval2 = new Short(val2); Short Shortval3 = new Short(val3); // returns less than 0 if this Short is less than the argument Short int cmp = Shortval1.compareTo(Shortval2); System.out.println("" + Shortval1 + " is less than " + Shortval2 + ", difference = " + cmp); // returns 0 if this Short is equal to the argument Short cmp = Shortval1.compareTo(Shortval3); System.out.println("" + Shortval1 + " is equal to " + Shortval3 + ", difference = " + cmp); // returns greater than if this Short is greater than the argument Short cmp = Shortval2.compareTo(Shortval1); System.out.println("" + Shortval2 + " is more than " + Shortval1 + ", difference = " + cmp); } }
让我们编译并运行上面的程序,这将产生下面的结果 −
50 is less than 200, difference = -150 50 is equal to 50, difference = 0 200 is more than 50, difference = 150