Java Integer toBinaryString() method
The toBinaryString() method of Java Integer class returns a string representing the specified int argument as an unsigned integer in base 2. Syntax public static String toBinaryString (int i) Parameters The parameter ‘i’ represents an integer to be converted to a string. Return Value This method returns the string which represents the unsigned integer value. Example 1
public class JavaIntegerToBinaryStringExample1 { public static void main(String[] args) { Integer val1=2; //the string representation of the unsigned integer value represented by the argument in binary (base 2) String val=Integer.toBinaryString(val1); System.out.println("Binary String : "+val); } }Output
Binary String : 10Example 2
public class JavaIntegerToBinaryStringExample2 { public static void main(String[] args) { //passing negative value Integer val1=-212; //the string representation of the unsigned integer value represented by the argument in binary (base 2) String val=Integer.toBinaryString(val1); System.out.println("Binary String : "+val); } }Output
Binary String : 11111111111111111111111100101100Example 3
public class JavaIntegerToBinaryStringExample3 { public static void main(String[] args) { //passing the maximum value Integer val1=Integer.MAX_VALUE; //the string representation of the unsigned integer value represented by the argument in binary (base 2) String val=Integer.toBinaryString(val1); System.out.println("Binary String : "+val); } }Output
Binary String : 1111111111111111111111111111111Example 4
public class JavaIntegerToBinaryStringExample4 { public static void main(String[] args) { //passing 0 Integer val1=0; //the string representation of the unsigned integer value represented by the argument in binary (base 2) String val=Integer.toBinaryString(val1); System.out.println("Binary String : "+val); } }Output
Binary String : 0