📜  Java Java类

📅  最后修改于: 2022-05-13 01:55:33.277000             🧑  作者: Mango

Java Java类

Short 类是原始类型 short 的包装类,它包含几个有效处理 short 值的方法,例如将其转换为字符串表示形式,反之亦然。 Short 类的对象可以保存一个短值。初始化一个 Short 对象主要有两个构造函数——

  • Short(short b):创建一个使用提供的值初始化的 Short 对象。
Syntax : public Short(short b)
Parameters :
b : value with which to initialize
  • Short(String s):创建一个使用字符串表示提供的短值初始化的 Short 对象。默认基数为 10。
Syntax : public Short(String s) 
                    throws NumberFormatException
Parameters :
s : string representation of the short value 
Throws :
NumberFormatException : If the string provided does not represent any short value.

方法:

  1. toString() :返回短值对应的字符串。
Syntax : public String toString(short b)
Parameters :
b : short value for which string representation required.
  1. valueOf() :返回使用提供的值初始化的 Short 对象。
Syntax : public static Short valueOf(short b)
Parameters :
b : a short value
  1. 另一个重载函数valueOf(String val,int 函数 ) 提供类似于
    新的 Short(Short.parseShort(val,radix))
Syntax : public static Short valueOf(String val, int radix)
            throws NumberFormatException
Parameters :
val : String to be parsed into short value
radix : radix to be used while parsing
Throws :
NumberFormatException : if String cannot be parsed to a short value in given radix.
  1. 另一个重载函数valueOf(String 函数 ) 提供类似于
    新的 Short(Short.parseShort(val,10))
Syntax : public static Short valueOf(String s)
           throws NumberFormatException
Parameters :
s : a String object to be parsed as short
Throws :
NumberFormatException : if String cannot be parsed to a short value in given radix.
  1. parseShort() :通过解析提供的基数字符串返回短值。与 valueOf() 不同,因为它返回一个原始的 short 值,而 valueOf() 返回 Short 对象。
Syntax : public static short parseShort(String val, int radix)
             throws NumberFormatException
Parameters :
val : String representation of short 
radix : radix to be used while parsing
Throws :
NumberFormatException : if String cannot be parsed to a short value in given radix.
  1. 另一个仅包含 String 作为参数的重载方法,radix 默认设置为 10。
Syntax : public static short parseShort(String val)
             throws NumberFormatException
Parameters :
val : String representation of short 
Throws :
NumberFormatException : if String cannot be parsed to a short value in given radix.
  1. decode() :返回一个 Short 对象,其中包含提供的字符串的解码值。提供的字符串必须采用以下形式,否则将抛出 NumberFormatException -
    Decimal-(符号)Decimal_Number
    Hex-(符号)”0x”Hex_Digits
    Hex-(符号)“0X”Hex_Digits
    八进制-(符号)”0”Octal_Digits
Syntax : public static Short decode(String s)
             throws NumberFormatException
Parameters :
s : encoded string to be parsed into short val
Throws :
NumberFormatException : If the string cannot be decoded into a short value
  1. byteValue() 返回与此 Short Object 对应的字节值。
Syntax : public byte byteValue()
  1. shortValue() 返回对应于这个 Short Object 的一个 short 值。
Syntax : public short shortValue()
  1. intValue() :返回与此 Short Object 对应的 int 值。
Syntax : public int intValue()
  1. longValue() 返回与此 Short Object 对应的 long 值。
Syntax : public long longValue()
  1. doubleValue() 返回与此短对象对应的双精度值。
Syntax : public double doubleValue()
  1. floatValue() 返回与此短对象对应的浮点值。
Syntax : public float floatValue()
  1. hashCode() :返回这个 Short Object 对应的 hashcode。
Syntax : public int hashCode()
  1. equals() 用于比较两个 Short 对象的相等性。如果两个对象都包含相同的短值,则此方法返回 true。只有在检查相等性时才应该使用。在所有其他情况下,应该首选 compareTo 方法。
Syntax : public boolean equals(Object obj)
Parameters :
obj : object to compare with
  1. compareTo() 用于比较两个 Short 对象的数值相等性。这应该在比较两个 Short 值的数值相等时使用,因为它会区分较小和较大的值。返回小于 0,0 的值,大于 0 的值表示小于、等于和大于。
Syntax : public int compareTo(Short b)
Parameters :
b : Short object to compare with
  1. compare() 用于比较两个原始短值是否相等。由于它是一个静态方法,因此可以在不创建任何 Short 对象的情况下使用它。
Syntax : public static int compare(short x,short y)
Parameters :
x : short value
y : another short value
  1. reverseBytes() 返回一个原始的短值,以给定短值的二进制补码形式反转位的顺序。
Syntax : public static short reverseBytes(short val)
Parameters :
val : short value whose bits to reverse in order.

Java
// Java program to illustrate
// various methods of Short class
public class Short_test
{
 
    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