Java Program to Convert Binary to Decimal Number
In Java, convert a binary string to decimal using
Integer.parseInt(binaryString, 2) or manually by processing each bit and summing powers of 2.Examples
Input101
Output5
Input1101
Output13
Input0
Output0
How to Think About It
To convert binary to decimal, think of each binary digit as a power of 2 starting from the right (0th power). Multiply each bit by 2 raised to its position index and add all results to get the decimal number.
Algorithm
1
Get the binary string input.2
Initialize a decimal result to 0.3
For each character in the binary string from left to right:4
Convert the character to a number (0 or 1).5
Multiply it by 2 raised to the power of its position from the right.6
Add this value to the decimal result.7
Return the decimal result.Code
java
public class BinaryToDecimal { public static void main(String[] args) { String binary = "1101"; int decimal = Integer.parseInt(binary, 2); System.out.println("Decimal value: " + decimal); } }
Output
Decimal value: 13
Dry Run
Let's trace the binary '1101' through the code.
1
Input binary string
binary = "1101"
2
Convert using Integer.parseInt
decimal = Integer.parseInt("1101", 2) = 13
3
Print result
Output: Decimal value: 13
| Binary Digit | Position (from right) | Value (digit * 2^position) |
|---|---|---|
| 1 | 3 | 1 * 8 = 8 |
| 1 | 2 | 1 * 4 = 4 |
| 0 | 1 | 0 * 2 = 0 |
| 1 | 0 | 1 * 1 = 1 |
Why This Works
Step 1: Parsing binary string
The method Integer.parseInt(binaryString, 2) reads the string as a base-2 number.
Step 2: Calculating decimal value
Each binary digit is multiplied by 2 raised to its position index and summed to get the decimal equivalent.
Step 3: Output the result
The decimal number is printed using System.out.println.
Alternative Approaches
Manual calculation with loop
java
public class BinaryToDecimalManual { public static void main(String[] args) { String binary = "1101"; int decimal = 0; int length = binary.length(); for (int i = 0; i < length; i++) { if (binary.charAt(length - 1 - i) == '1') { decimal += Math.pow(2, i); } } System.out.println("Decimal value: " + decimal); } }
This method shows the step-by-step calculation but is less concise than using built-in parsing.
Using Integer.valueOf
java
public class BinaryToDecimalValueOf { public static void main(String[] args) { String binary = "1101"; int decimal = Integer.valueOf(binary, 2); System.out.println("Decimal value: " + decimal); } }
Similar to parseInt but returns an Integer object; useful if you need an object instead of primitive int.
Complexity: O(n) time, O(1) space
Time Complexity
The conversion processes each binary digit once, so time grows linearly with input length.
Space Complexity
Only a few variables are used, so space is constant regardless of input size.
Which Approach is Fastest?
Using Integer.parseInt is fastest and simplest; manual loops are educational but slower.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Integer.parseInt | O(n) | O(1) | Quick and simple conversion |
| Manual loop calculation | O(n) | O(1) | Learning and understanding process |
| Integer.valueOf | O(n) | O(1) | When Integer object needed |
Use
Integer.parseInt(binaryString, 2) for quick and reliable binary to decimal conversion.Forgetting to specify base 2 in
parseInt causes wrong decimal results.