Java - Integer Class toBinaryString() Method

Integer class toBinaryString() method: Here, we are going to learn about the toBinaryString() method of Integer class with its syntax and example. By Preeti Jain Last updated : March 18, 2024

Integer class toBinaryString() method

  • toBinaryString() method is available in java.lang package.
  • toBinaryString() method is used to represent a binary string of the given parameter [value] of integer type as an unsigned integer in binary (base 2).
  • toBinaryString() method is a static method, it is accessible with the class name too and if we try to access the method with the class object then also we will not get an error.
  • toBinaryString() method does not throw an exception at the time of conversion from integer to a binary string.

Syntax

public static String ToBinaryString(int value);

Parameters

  • int value – represents the integer value to be converted.

Return Value

The return type of this method is int, it returns the binary string of the given parameter that represent the unsigned integer value.

Example

// 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);
    }
}

Output

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

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.