0
0
JavaHow-ToBeginner · 2 min read

Java Program to Convert Decimal to Binary

In Java, you can convert a decimal number to binary using Integer.toBinaryString(decimalNumber) or by repeatedly dividing the number by 2 and collecting remainders.
📋

Examples

Input5
Output101
Input10
Output1010
Input0
Output0
🧠

How to Think About It

To convert decimal to binary, think of dividing the decimal number by 2 repeatedly and noting the remainder each time. These remainders, read in reverse order, form the binary number. Alternatively, Java provides a built-in method to do this conversion easily.
📐

Algorithm

1
Get the decimal number as input.
2
If the number is 0, return "0" as binary.
3
While the number is greater than 0, divide it by 2 and store the remainder.
4
Collect all remainders in reverse order to form the binary string.
5
Return the binary string.
💻

Code

java
public class DecimalToBinary {
    public static void main(String[] args) {
        int decimal = 10;
        String binary = Integer.toBinaryString(decimal);
        System.out.println("Binary of " + decimal + " is: " + binary);
    }
}
Output
Binary of 10 is: 1010
🔍

Dry Run

Let's trace converting decimal 5 to binary using repeated division.

1

Divide 5 by 2

5 / 2 = 2 remainder 1

2

Divide 2 by 2

2 / 2 = 1 remainder 0

3

Divide 1 by 2

1 / 2 = 0 remainder 1

QuotientRemainder
51
20
11
💡

Why This Works

Step 1: Using Integer.toBinaryString

Java's Integer.toBinaryString method converts the decimal number directly to its binary string representation.

Step 2: Manual division method

Dividing the decimal number by 2 repeatedly and collecting remainders builds the binary digits from least significant to most significant.

Step 3: Reversing remainders

Reading the collected remainders in reverse order gives the correct binary number.

🔄

Alternative Approaches

Manual conversion using StringBuilder
java
public class DecimalToBinaryManual {
    public static void main(String[] args) {
        int decimal = 10;
        StringBuilder binary = new StringBuilder();
        int num = decimal;
        if (num == 0) {
            binary.append('0');
        }
        while (num > 0) {
            binary.append(num % 2);
            num /= 2;
        }
        System.out.println("Binary of " + decimal + " is: " + binary.reverse().toString());
    }
}
This method shows the step-by-step process but is longer and less concise than the built-in method.
Using Java 8 Streams (advanced)
java
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class DecimalToBinaryStream {
    public static void main(String[] args) {
        int decimal = 10;
        String binary = IntStream.iterate(decimal, n -> n > 0, n -> n / 2)
            .map(n -> n % 2)
            .mapToObj(String::valueOf)
            .collect(Collectors.collectingAndThen(Collectors.toList(), lst -> {
                java.util.Collections.reverse(lst);
                return String.join("", lst);
            }));
        System.out.println("Binary of " + decimal + " is: " + binary);
    }
}
Uses streams for functional style but is more complex and less readable for beginners.

Complexity: O(log n) time, O(log n) space

Time Complexity

The conversion requires dividing the number by 2 repeatedly until it reaches 0, which takes about log base 2 of n steps.

Space Complexity

Storing the binary digits requires space proportional to the number of bits, which is also about log base 2 of n.

Which Approach is Fastest?

Using Integer.toBinaryString is fastest and most efficient, while manual methods are educational but slower and more error-prone.

ApproachTimeSpaceBest For
Integer.toBinaryStringO(log n)O(log n)Quick and simple conversion
Manual division with StringBuilderO(log n)O(log n)Learning and understanding process
Java Streams methodO(log n)O(log n)Functional programming style
💡
Use Integer.toBinaryString() for quick and easy decimal to binary conversion in Java.
⚠️
Beginners often forget to reverse the collected remainders when converting manually, resulting in incorrect binary output.