JAVAJava中的短类

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

null
  • 短(短b): 创建一个用提供的值初始化的短对象。
Syntax : public Short(short b)Parameters :b : value with which to initialize
  • 短(字符串s): 创建一个用字符串表示提供的短值初始化的短对象。默认基数为10。
Syntax : public Short(String s)                     throws NumberFormatExceptionParameters :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(): 返回用提供的值初始化的短对象。
Syntax : public static Short valueOf(short b)Parameters :b : a short value
  1. 另一个重载函数valueOf(String val,int-radix),它提供与 新Short(Short.parseShort(val,基数))
Syntax : public static Short valueOf(String val, int radix)            throws NumberFormatExceptionParameters :val : String to be parsed into short valueradix : radix to be used while parsingThrows :NumberFormatException : if String cannot be parsed to a short value in given radix.
  1. 另一个重载函数valueOf(String val),提供与 新的Short(Short.parseShort(val,10))
Syntax : public static Short valueOf(String s)           throws NumberFormatExceptionParameters :s : a String object to be parsed as shortThrows :NumberFormatException : if String cannot be parsed to a short value in given radix.
  1. parseShort(): 通过以提供的基数解析字符串返回短值。与valueOf()不同,因为它返回一个基本的短值,valueOf()返回短对象。
Syntax : public static short parseShort(String val, int radix)             throws NumberFormatExceptionParameters :val : String representation of short radix : radix to be used while parsingThrows :NumberFormatException : if String cannot be parsed to a short value in given radix.
  1. 另一个重载方法只包含字符串作为参数,基数默认设置为10。
Syntax : public static short parseShort(String val)             throws NumberFormatExceptionParameters :val : String representation of short Throws :NumberFormatException : if String cannot be parsed to a short value in given radix.
  1. 解码(): 返回一个包含所提供字符串的解码值的短对象。提供的字符串必须为以下形式,否则将抛出NumberFormatException- 十进制(符号)十进制数 十六进制-(符号)“0x”十六进制数字 十六进制-(符号)“0X”十六进制数字 八进制-(符号)“0”八进制数字
Syntax : public static Short decode(String s)             throws NumberFormatExceptionParameters :s : encoded string to be parsed into short valThrows :NumberFormatException : If the string cannot be decoded into a short value
  1. 字节值() : 返回与此短对象对应的字节值。
Syntax : public byte byteValue()
  1. shortValue() : 返回与此短对象对应的短值。
Syntax : public short shortValue()
  1. intValue(): 返回与此短对象对应的int值。
Syntax : public int intValue()
  1. longValue() : 返回与此短对象对应的长值。
Syntax : public long longValue()
  1. doubleValue() : 返回与此短对象对应的双精度值。
Syntax : public double doubleValue()
  1. 浮动值() : 返回与此短对象对应的浮点值。
Syntax : public float floatValue()
  1. hashCode() : 返回与此短对象对应的哈希代码。
Syntax : public int hashCode()
  1. 等于 : 用来比较两个短物体的相等性。如果两个对象都包含相同的短值,则此方法返回true。仅当检查是否相等时才应使用。在所有其他情况下,应首选比较法。
Syntax : public boolean equals(Object obj)Parameters :obj : object to compare with
  1. 比较 : 用于比较两个短对象的数值相等性。在比较两个短值的数值相等性时,应使用此选项,因为它将区分较小值和较大值。返回小于0,0的值,对于小于、等于和大于,返回大于0的值。
Syntax : public int compareTo(Short b)Parameters :b : Short object to compare with
  1. 比较 : 用于比较两个基本短值的数值相等性。由于它是一种静态方法,因此可以在不创建任何短期对象的情况下使用。
Syntax : public static int compare(short x,short y)Parameters :x : short valuey : another short value
  1. 反向字节() : 返回一个基元短值,该基元短值与给定短值的补位形式的位顺序相反。
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) = 55valueOf(b) = 55ValueOf(bb) = 45ValueOf(bb,6) = 29parseShort(bb) = 45parseShort(bb,6) = 29decode(45) = 45decode(005) = 5decode(0x0f) = 15bytevalue(x) = 55shortvalue(x) = 55intvalue(x) = 55longvalue(x) = 55doublevalue(x) = 55.0floatvalue(x) = 55.0hashcode(x) = 55x.equals(y) = falsecompare(x,y) = 10x.compareTo(y) = 10Short.reverseBytes(to_rev) = 11520

本文由 Rishabh Mahrsee .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞11 分享