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