java.lang.Byte.toString()方法範例


java.lang.Byte.toString() 返回一個代表此位元組的String物件的值。該值被轉換為符號的十進位制表示法,並以字串形式返回,完全一樣,如果位元組值被賦予作為引數傳遞給了 toString(byte) 方法。

宣告

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

public String toString()

Overrides

toString 在 Object 類

引數

  • NA

返回值

此方法將返回這個物件的基數為10的值的字串表示形式。

異常

  • NA

例子

下面的例子顯示了lang.Byte.toString()方法的使用。

package com.yiibai;

import java.lang.*;

public class ByteDemo {

   public static void main(String[] args) {

      // create 2 Byte objects b1, b2
      Byte b1, b2;

      // create 2 String's s1,s2
      String s1, s2;

      // assign values to b1, b2
      b1 = new Byte("123");
      b2 = new Byte("-123");

      // assign toString values of b1, b2 to s1, s2
      s1 = b1.toString();
      s2 = b2.toString();

      String str1 = "String value of Byte " + b1 + " is " + s1;
      String str2 = "String value of Byte " + b2 + " is " + s2;

      // print s1, s2 values
      System.out.println( str1 );
      System.out.println( str2 );
   }
}

讓我們來編譯和執行上面的程式,這將產生以下結果:

String value of Byte 123 is 123
String value of Byte -123 is -123