Java 简明教程

Java - Byte class with Examples

Introduction

Java Byte 类用一个对象包装一个基本类型的字节值。类型为 Byte 的对象包含一个类型为字节的字段。

The Java Byte class class wraps a value of primitive type byte in an object. An object of type Byte contains a single field whose type is byte.

Class Declaration

以下是对 java.lang.Byte 类的声明 −

Following is the declaration for java.lang.Byte class −

public final class Byte
   extends Number
      implements Comparable<Byte>

Field

以下是 java.lang.Byte 类的字段 −

Following are the fields for java.lang.Byte class −

  1. static byte MAX_VALUE − This is constant holding the maximum value a byte can have, 27-1.

  2. static byte MIN_VALUE − This is constant holding the minimum value a byte can have, -27.

  3. static int SIZE − This is the number of bits used to represent a byte value in two’s complement binary form.

  4. static Class<Byte> TYPE − This is the Class instance representing the primitive type byte.

Class constructors

Class methods

Methods inherited

此类从以下类中继承方法:

This class inherits methods from the following classes −

  1. java.lang.Object

Example

以下示例演示了 Byte 类用于获取来自字符串的字节的用法。

The following example shows the usage of Byte class to get byte from a string.

package com.tutorialspoint;

public class ByteDemo {

   public static void main(String[] args) {

      // create a String s and assign value to it
      String s = "+120";

      // create a Byte object b
      Byte b;

      // get the value of byte from string
      b = Byte.valueOf(s);

      // print the value
      System.out.println( "Byte value of string " + s + " is " + b );
   }
}

让我们编译并运行上述程序,这将生成以下结果 −

Let us compile and run the above program, this will produce the following result −

Byte value of string +120 is 120