Java Byte类相关方法调用示例

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

Byteclass在一个对象中包装了一个原始类型为byte的值。一个Bytec类型的对象包含一个类型为字节的字段。

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

Byte类 构造函数

  • Byte(byte value) - 构建一个新分配的Byte对象,代表指定的字节值。
  • Byte(String s) - 构建一个新分配的Byte对象,代表String参数所表示的字节值。

例子:

byte b = 55;
        String str = "45";
          
        // Construct two Byte objects
        Byte x = new Byte(b);
        Byte y = new Byte(str);

Byte类方法

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

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

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

//*/*
/* This class to demonstrate Byte wrapper class methods
/* @author javaguides
/*
/*/
public class ByteClassExample {
 public static void main(String[] args) {
  byte b = 55;
        String str = "45";
          
        // Construct two Byte objects
        Byte x = new Byte(b);
        Byte y = new Byte(str);
  
        // toString()
        System.out.println("toString(b) = " + Byte.toString(b));
  
        // valueOf()
        // return Byte object
        Byte z = Byte.valueOf(b);
        System.out.println("valueOf(b) = " + z);
        z = Byte.valueOf(str);
        System.out.println("ValueOf(bb) = " + z);
        z = Byte.valueOf(str, 6);
        System.out.println("ValueOf(bb,6) = " + z);
  
        // parseByte()
        // return primitive byte value
        byte zz = Byte.parseByte(str);
        System.out.println("parseByte(bb) = " + zz);
        zz = Byte.parseByte(str, 6);
        System.out.println("parseByte(bb,6) = " + zz);
          
        //decode()
        String decimal = "45";
        String octal = "005";
        String hex = "0x0f";
          
        Byte dec=Byte.decode(decimal);
        System.out.println("decode(45) = " + dec);
        dec=Byte.decode(octal);
        System.out.println("decode(005) = " + dec);
        dec=Byte.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=Byte.compare(x, y);
        System.out.println("compare(x,y) = " + e);
          
        int f=x.compareTo(y);
        System.out.println("x.compareTo(y) = " + f);
 }
}

输出。

toString(b) = 55
valueOf(b) = 55
ValueOf(bb) = 45
ValueOf(bb,6) = 29
parseByte(bb) = 45
parseByte(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

###参考

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

相关文章

微信公众号

最新文章

更多