java.lang.Short.equals()方法實例
java.lang.Short.equals() 方法比較此對象與指定對象。其結果是true,當且僅當參數不為null,並且包含相同的short值作為此short 對象。
聲明
以下是java.lang.Short.equals()方法的聲明
public boolean equals(Object obj)
參數
-
obj -- 這是要比較的對象。
返回值
如果對象是相同此方法返回true,否則返回false。
異常
-
NA
例子
下麵的例子顯示java.lang.Short.equals()方法的使用。
package com.yiibai; import java.lang.*; public class ShortDemo { public static void main(String[] args) { // create short object and assign value to it short shortNum1 = 200, shortNum2 = 200, shortNum3 = 50; Short ShortObj1 = new Short(shortNum1); Short ShortObj2 = new Short(shortNum2); Short ShortObj3 = new Short(shortNum3); // returs false as both object contains different short value boolean val = ShortObj1.equals(ShortObj3); System.out.println("shortObj1 and shortObj3 have same value : " + val); // returns true as both object contains same short value val = ShortObj1.equals(ShortObj2); System.out.println("shortObj1 and shortObj2 have same value : " + val); } }
讓我們來編譯和運行上麵的程序,這將產生以下結果:
shortObj1 and shortObj3 have same value : false shortObj1 and shortObj2 have same value : true