Java - Byte toString() 方法



描述

Java Byte toString(byte b) 方法返回一個新的字串物件,表示指定的位元組。基數假定為 10。

宣告

以下是 java.lang.Byte.toString() 方法的宣告

public static String toString(byte b)

引數

b − 要轉換的位元組

返回值

此方法返回指定位元組的字串表示形式。

異常

獲取正位元組的字串表示形式示例

以下示例演示了 Byte toString(byte) 方法的使用。我們建立了一個位元組變數併為其賦值。然後建立一個字串變數,並使用 Byte.toString(String) 方法列印位元組值的字串表示形式。

package com.tutorialspoint;
public class ByteDemo {
   public static void main(String[] args) {

      // create a byte primitive bt and asign value
      byte bt = 20;

      // create a String s
      String s;

      /**
       *  static method is called using class name. Assign 
       *  string representation of bt to s
       */
      s = Byte.toString(bt);

      String str = "String representation of byte primitive " +bt+ " is " +s;

      // print s value
      System.out.println( str );
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

String representation of byte primitive 20 is 20

獲取負位元組的字串表示形式示例

以下示例演示了 Byte toString(byte) 方法的使用。我們建立了一個位元組變數併為其賦值一個負值。然後建立一個字串變數,並使用 Byte.toString(String) 方法列印位元組值的字串表示形式。

package com.tutorialspoint;
public class ByteDemo {
   public static void main(String[] args) {

      // create a byte primitive bt and asign value
      byte bt = -20;

      // create a String s
      String s;

      /**
       *  static method is called using class name. Assign 
       *  string representation of bt to s
       */
      s = Byte.toString(bt);

      String str = "String representation of byte primitive " +bt+ " is " +s;

      // print s value
      System.out.println( str );
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

String representation of byte primitive -20 is -20

獲取零位元組的字串表示形式示例

以下示例演示了 Byte toString(byte) 方法的使用。我們建立了一個位元組變數併為其賦值零。然後建立一個字串變數,並使用 Byte.toString(String) 方法列印位元組值的字串表示形式。

package com.tutorialspoint;
public class ByteDemo {
   public static void main(String[] args) {

      // create a byte primitive bt and asign value
      byte bt = 0;

      // create a String s
      String s;

      /**
       *  static method is called using class name. Assign 
       *  string representation of bt to s
       */
      s = Byte.toString(bt);

      String str = "String representation of byte primitive " +bt+ " is " +s;

      // print s value
      System.out.println( str );
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

String representation of byte primitive 0 is 0
java_lang_byte.htm
廣告