當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java Integer toBinaryString()用法及代碼示例

Integer類toBinaryString()方法

  • toBinaryString() 方法可在java.lang包。
  • toBinaryString() 方法用於將整數類型的給定參數 [value] 的二進製字符串表示為二進製(基數為 2)的無符號整數。
  • toBinaryString() 方法是一個靜態方法,它也可以通過類名訪問,如果我們嘗試使用類對象訪問該方法,那麽我們也不會收到錯誤。
  • toBinaryString() 方法從整數轉換為二進製字符串時不會拋出異常。

用法:

    public static String ToBinaryString(int value);

參數:

  • int value– 表示要轉換的整數值。

返回值:

這個方法的返回類型是int,它返回表示無符號整數值的給定參數的二進製字符串。

例:

// Java program to demonstrate the example 
// of toBinaryString(int value) method of Integer class

public class ToBinaryStringOfIntegerClass {
    public static void main(String[] args) {
        // Variables initialization
        int i1 = 10;
        int i2 = 20;
        int i3 = 30;
        int i4 = Integer.MAX_VALUE;
        int i5 = Integer.MIN_VALUE;

        // Integer instance creation
        Integer value = new Integer(i1);

        // It represents binary string of the given
        // integer type i2 argument
        String s = value.toBinaryString(i2);

        // Display Binary String Representation
        System.out.println("value.toBinaryString(i2):" + s);

        // It represents binary string of the given
        // integer type i3 argument
        s = value.toBinaryString(i3);

        // Display Binary String Representation
        System.out.println("value.toBinaryString(i3):" + s);

        // It represents binary string of the given
        // integer type i4 argument
        s = value.toBinaryString(i4);

        // Display Binary String Representation
        System.out.println("value.toBinaryString(i4):" + s);

        // It represents binary string of the given
        // integer type i5 argument
        s = value.toBinaryString(i5);

        // Display Binary String Representation
        System.out.println("value.toBinaryString(i5):" + s);
    }
}

輸出

value.toBinaryString(i2):10100
value.toBinaryString(i3):11110
value.toBinaryString(i4):1111111111111111111111111111111
value.toBinaryString(i5):10000000000000000000000000000000


相關用法


注:本文由純淨天空篩選整理自Preeti Jain大神的英文原創作品 Java Integer class toBinaryString() method with example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。