Java Short equals()方法

java.lang.Short.equals() 方法比较此对象与指定对象。其结果是true,当且仅当参数不为null,并且包含相同的short值作为此short 对象。

1 语法

public boolean equals(Object obj)

2 参数

obj :这是要比较的对象。

3 返回值

如果对象是相同此方法返回true,否则返回false。

4 示例 

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * Java Short equals()方法
 */
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

 

热门文章

优秀文章