Java Integer toBinaryString() Method
Example
Write numbers in binary:
public class Main {
public static void main(String[] args) {
System.out.println(Integer.toBinaryString(5));
System.out.println(Integer.toBinaryString(10));
System.out.println(Integer.toBinaryString(255));
}
}
Definition and Usage
The toBinaryString() method returns a string with the number written in binary (base 2, with only the digits 0 and 1).
For a negative number, the result has 32 digits, because it shows the 32 bits (binary digits) that Java uses to store an int.
Tip: Use toHexString() for hexadecimal and toOctalString() for octal.
Syntax
public static String toBinaryString(int i)
Parameter Values
| Parameter | Description |
|---|---|
| i | Required. The number to convert. |
Technical Details
| Returns: | A String with the number in binary. |
|---|
Related Pages
❮ Integer Methods