Java Short类相关方法调用示例

x33g5p2x  于2021-08-19 转载在 Java  
字(2.7k)|赞(0)|评价(0)|浏览(187)

Short类将一个原始类型为short的值包装在一个对象中。Short类型的对象包含一个类型为short的单一字段。

此外,该类还提供了几种方法,用于将short转换为String,将String转换为short,以及其他处理short时有用的常量和方法。

Short类构造函数

  • Short(short value)- 构建一个新分配的Short对象,代表指定的short值。
  • Short(String s)- 构建一个新分配的Short对象,代表由String参数指示的短值。
    例子:
short b = 55;
String bb = "45";
 
// Construct two Short objects
Short x = new Short(b);
Short y = new Short(bb);

Short类方法

类图显示了Short类所提供的API/方法的列表。

在这篇文章中,我们将学习Shortwrapper类的几个重要方法。

**例子:**这个程序演示了Short封装类方法的用法。

package com.javaguides.corejava.wrapperclasses;

//*/*
/* This class to demonstrate Short wrapper class methods
/* @author javaguides.net
/*
/*/
public class ShortClassExample {
 public static void main(String[] args) {
  short b = 55;
        String bb = "45";
         
        // Construct two Short objects
        Short x = new Short(b);
        Short y = new Short(bb);
 
        // toString()
        System.out.println("toString(b) = " + Short.toString(b));
 
        // valueOf()
        // return Short object
        Short z = Short.valueOf(b);
        System.out.println("valueOf(b) = " + z);
        z = Short.valueOf(bb);
        System.out.println("ValueOf(bb) = " + z);
        z = Short.valueOf(bb, 6);
        System.out.println("ValueOf(bb,6) = " + z);
 
        // parseShort()
        // return primitive short value
        short zz = Short.parseShort(bb);
        System.out.println("parseShort(bb) = " + zz);
        zz = Short.parseShort(bb, 6);
        System.out.println("parseShort(bb,6) = " + zz);
         
        //decode()
        String decimal = "45";
        String octal = "005";
        String hex = "0x0f";
         
        Short dec = Short.decode(decimal);
        System.out.println("decode(45) = " + dec);
        dec = Short.decode(octal);
        System.out.println("decode(005) = " + dec);
        dec = Short.decode(hex);
        System.out.println("decode(0x0f) = " + dec);
 
        System.out.println("bytevalue(x) = " + x.byteValue());
        System.out.println("shortvalue(x) = " + x.shortValue());
        System.out.println("intvalue(x) = " + x.intValue());
        System.out.println("longvalue(x) = " + x.longValue());
        System.out.println("doublevalue(x) = " + x.doubleValue());
        System.out.println("floatvalue(x) = " + x.floatValue());
         
        int hash = x.hashCode();
        System.out.println("hashcode(x) = " + hash);
         
        boolean eq = x.equals(y);
        System.out.println("x.equals(y) = " + eq);
         
        int e = Short.compare(x, y);
        System.out.println("compare(x,y) = " + e);
         
        int f = x.compareTo(y);
        System.out.println("x.compareTo(y) = " + f);
         
         
        short to_rev = 45;
        System.out.println("Short.reverseBytes(to_rev) = " + Short.reverseBytes(to_rev));
 }
}

输出。

toString(b) = 55
valueOf(b) = 55
ValueOf(bb) = 45
ValueOf(bb,6) = 29
parseShort(bb) = 45
parseShort(bb,6) = 29
decode(45) = 45
decode(005) = 5
decode(0x0f) = 15
bytevalue(x) = 55
shortvalue(x) = 55
intvalue(x) = 55
longvalue(x) = 55
doublevalue(x) = 55.0
floatvalue(x) = 55.0
hashcode(x) = 55
x.equals(y) = false
compare(x,y) = 10
x.compareTo(y) = 10
Short.reverseBytes(to_rev) = 11520

###参考资料

https://docs.oracle.com/javase/8/docs/api/java/lang/Short.html

相关文章

最新文章

更多