Binary to Decimal in Java: Explanation & Practice
Convert a binary string to its decimal equivalent
Problem summary
Write a program that converts a binary number (as a string) to its decimal equivalent.
Starter code
public class Main {
public static void main(String[] args) {
String binary = "1010"; // Test case 1
// Convert binary to decimal
// Print: Decimal: <result>
}
}Expected output and test cases
- 1010 = 10
Decimal: 10
- 1111 = 15
Decimal: 15
- 11111111 = 255
Decimal: 255
Hints
- Process each bit from right to left
- Multiply bit by 2^position
- Use Integer.parseInt(binary, 2) for built-in solution
Related Core Java Basics exercises
- Practice Strong Number Check in Java
- Practice Automorphic Number Check in Java
- Practice Decimal to Binary in Java
Practice all Core Java Basics exercises · Run this idea in the Java compiler