Java.lang Package Classes

Java Integer - toBinaryString() Method



The java.lang.Integer.toBinaryString() method returns a string representation of the integer argument as an unsigned integer in base 2.

Syntax

public static String toBinaryString(int i)

Parameters

i Specify a integer to be converted to a string.

Return Value

Returns the string representation of the unsigned integer value represented by the argument in binary (base 2).

Exception

NA.

Example:

In the example below, the java.lang.Integer.toBinaryString() method returns a string representation of the integer argument as an unsigned integer in base 2.

import java.lang.*;

public class MyClass {
  public static void main(String[] args) {
    
    //creating int value
    int x = 25;
    int y = 31;
    int z = 111;

    //creating and printing string representation of 
    //argument in binary system.
    System.out.println("x in binary system is: " + Integer.toBinaryString(x)); 
    System.out.println("y in binary system is: " + Integer.toBinaryString(y)); 
    System.out.println("z in binary system is: " + Integer.toBinaryString(z));   
  }
}

The output of the above code will be:

x in binary system is: 11001
y in binary system is: 11111
z in binary system is: 1101111

❮ Java.lang - Integer