eBook – Guide Spring Cloud – NPI EA (cat=Spring Cloud)
announcement - icon

Let's get started with a Microservice Architecture with Spring Cloud:

>> Join Pro and download the eBook

eBook – Mockito – NPI EA (tag = Mockito)
announcement - icon

Mocking is an essential part of unit testing, and the Mockito library makes it easy to write clean and intuitive unit tests for your Java code.

Get started with mocking and improve your application tests using our Mockito guide:

Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Reactive – NPI EA (cat=Reactive)
announcement - icon

Spring 5 added support for reactive programming with the Spring WebFlux module, which has been improved upon ever since. Get started with the Reactor project basics and reactive programming in Spring Boot:

>> Join Pro and download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Jackson – NPI EA (cat=Jackson)
announcement - icon

Do JSON right with Jackson

Download the E-book

eBook – HTTP Client – NPI EA (cat=Http Client-Side)
announcement - icon

Get the most out of the Apache HTTP Client

Download the E-book

eBook – Maven – NPI EA (cat = Maven)
announcement - icon

Get Started with Apache Maven:

Download the E-book

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

eBook – RwS – NPI EA (cat=Spring MVC)
announcement - icon

Building a REST API with Spring?

Download the E-book

Course – LS – NPI EA (cat=Jackson)
announcement - icon

Get started with Spring and Spring Boot, through the Learn Spring course:

>> LEARN SPRING
Course – RWSB – NPI EA (cat=REST)
announcement - icon

Explore Spring Boot 3 and Spring 6 in-depth through building a full REST API with the framework:

>> The New “REST With Spring Boot”

Course – LSS – NPI EA (cat=Spring Security)
announcement - icon

Yes, Spring Security can be complex, from the more advanced functionality within the Core to the deep OAuth support in the framework.

I built the security material as two full courses - Core and OAuth, to get practical with these more complex scenarios. We explore when and how to use each feature and code through it on the backing project.

You can explore the course here:

>> Learn Spring Security

Course – LSD – NPI EA (tag=Spring Data JPA)
announcement - icon

Spring Data JPA is a great way to handle the complexity of JPA with the powerful simplicity of Spring Boot.

Get started with Spring Data JPA through the guided reference course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – LJB – NPI EA (cat = Core Java)
announcement - icon

Code your way through and build up a solid, practical foundation of Java:

>> Learn Java Basics

Partner – LambdaTest – NPI EA (cat= Testing)
announcement - icon

Distributed systems often come with complex challenges such as service-to-service communication, state management, asynchronous messaging, security, and more.

Dapr (Distributed Application Runtime) provides a set of APIs and building blocks to address these challenges, abstracting away infrastructure so we can focus on business logic.

In this tutorial, we'll focus on Dapr's pub/sub API for message brokering. Using its Spring Boot integration, we'll simplify the creation of a loosely coupled, portable, and easily testable pub/sub messaging system:

>> Flexible Pub/Sub Messaging With Spring Boot and Dapr

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

However, this approach doesn’t handle negative integers correctly. It produced an empty string for negative inputs. For example, if we pass -2 to this method, the loop won’t execute since n is less than 0. As a result, the method returns an empty string for a negative value:

int negativeN = -2;
String negativeBinaryString = IntegerToBinary.convertIntegerToBinary(negativeN);
assertEquals("", negativeBinaryString);

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

In this approach, Integer.toBinaryString(int i) converts an integer to its binary representation. It does this for  negative numbers as well. For negative numbers, it returns the 32-bit two’s complement representation. For instance, for -2, it returns 11111111111111111111111111111110:

int negativeN = -2;
String negativeBinaryString = Integer.toBinaryString(negativeN);
assertEquals("11111111111111111111111111111110", negativeBinaryString);

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, we passed the radix value of 2 while calling the Integer#toString method to convert the integer n into its binary string representation.

In addition, Integer.toString(int i, int radix) converts an integer to a string representation with a specified radix. For radix 2, it returns a binary string. However, for negative numbers, it includes a negative sign. Therefore, with a negative value of -2, the result is -10, which isn’t a full two’s complement representation but rather a simple binary conversion with a negative sign:

int negativeN = -2;
String negativeBinaryString = Integer.toString(negativeN, 2);
assertEquals("-10", negativeBinaryString);

5. Getting the N-Bit (Zero-Padded) Binary Representation

We’ve learned how to get an integer’s binary format string. Sometimes, we’d like to have a 4-bit, 8-bit, or 16-bit binary representation. For example, here’s what the integer 7 (111) ‘s 4-, 8- and 16-bit representations look like:

  • 4-bit: 0111
  • 8-bit: 00000111
  • 16-bit: 0000000000000111

Next, let’s take the 8-bit format as an example to see how to get the binary string in the required format.

One way to achieve that is to use the String#format() function to format the binary string into a space-padded string. Then, we replace each space character with a 0:

int n = 7;
String binaryString = String.format("%8s", Integer.toBinaryString(n)).replace(" ","0");
assertEquals("00000111", binaryString);

Some popular libraries also offer the padding helper methods. For example, the StringUtils class from Apache Commons Lang 3 provides the leftPad() method, which allows us to left-pad a string with specified characters easily:

int n = 7;
String binaryString = StringUtils.leftPad(Integer.toBinaryString(n), 8, "0");
assertEquals("00000111", binaryString);

6. Conclusion

In this article, 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.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Baeldung Pro – NPI EA (cat = Baeldung)
announcement - icon

Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:

>> Explore a clean Baeldung

Once the early-adopter seats are all used, the price will go up and stay at $33/year.

eBook – HTTP Client – NPI EA (cat=HTTP Client-Side)
announcement - icon

The Apache HTTP Client is a very robust library, suitable for both simple and advanced use cases when testing HTTP endpoints. Check out our guide covering basic request and response handling, as well as security, cookies, timeouts, and more:

>> Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

Course – LS – NPI EA (cat=REST)

announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

eBook Jackson – NPI EA – 3 (cat = Jackson)