Home » Java programming language

Java Integer class toBinaryString() method with example

Integer class toBinaryString() method: Here, we are going to learn about the toBinaryString() method of Integer class with its syntax and example.
Submitted by Preeti Jain, on September 28, 2019

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

Parameter(s):

  • 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

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.