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

  1. Process each bit from right to left
  2. Multiply bit by 2^position
  3. Use Integer.parseInt(binary, 2) for built-in solution

Related Core Java Basics exercises

Practice all Core Java Basics exercises · Run this idea in the Java compiler