Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

November Discount Launch 2022 – Top
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:

>> GET ACCESS NOW

November Discount Launch 2022 – TEMP TOP (NPI)
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:

>> GET ACCESS NOW

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

1. Overview

In this tutorial, we'll look briefly at the different ways of printing an integer in binary format in Java.

First, we'll take a conceptual look. And then, we'll learn some built-in Java functions for conversion.

2. Using Integer to Binary Conversion

In this section, we'll write our custom method to convert an integer into a binary format string in Java. Before writing the code, let's first understand how to convert an integer into a binary format.

To convert an integer n into its binary format, we need to:

  1. Store the remainder when number n is divided by 2 and update the number n with the value of the quotient
  2. Repeat step 1 until the number n is greater than zero
  3. Finally, print the remainders in reverse order

Let's see an example of converting 7 into its binary format equivalent:

  1. First, divide 7 by 2: remainder 1, quotient 3
  2. Second, divide 3 by 2: remainder 1, quotient 1
  3. Then, divide 1 by 2: remainder 1, quotient 0
  4. And finally, print the remainders in reverse order since the quotient in the previous step is 0: 111

Next, let's implement the above algorithm:

public static String convertIntegerToBinary(int n) {
    if (n == 0) {
        return "0";
    }
    StringBuilder binaryNumber = new StringBuilder();
    while (n > 0) {
        int remainder = n % 2;
        binaryNumber.append(remainder);
        n /= 2;
    }
    binaryNumber = binaryNumber.reverse();
    return binaryNumber.toString();
}

3. Using Integer#toBinaryString Method

Java's Integer class has a method named toBinaryString to convert an integer into its binary equivalent string.

Let's look at the signature of the Integer#toBinaryString method:

public static String toBinaryString(int i)

It takes an integer argument and returns a binary string representation of that integer:

int n = 7;
String binaryString = Integer.toBinaryString(n);
assertEquals("111", binaryString);

4. Using Integer#toString Method

Now, let's look at the signature of the Integer#toString method:

public static String toString(int i, int radix)

The Integer#toString method is an in-built method in Java that takes two arguments. First, it takes an integer that is to be converted to a string. Second, it takes radix that is to be used while converting the integer into its string representation.

It returns a string representation of the integer input in the base specified by the radix.

Let's use this method to convert an integer into its binary format using a radix value of 2:

int n = 7;
String binaryString = Integer.toString(n, 2);
assertEquals("111", binaryString);

As we can see that we passed the radix value of 2 while calling the Integer#toString method to convert the integer n into its binary string representation.

5. Conclusion

In conclusion, we looked at integer to binary conversion. Furthermore, we saw a couple of built-in Java methods to convert an integer into a string in binary format.

As always, all these code samples are available over on GitHub.

November Discount Launch 2022 – Bottom
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:

>> GET ACCESS NOW

Generic footer banner
Comments are closed on this article!