java examples
Home Java Examples Resources Java Interview Questions
Brainpower SEO

How to Convert Decimal to Binary?

package com.javacoderanch.example.java.lang;

public class IntegerToBinaryExample 
{
    public static void main(String[] args)
    {
        int integer = 127;
        String binary = Integer.toBinaryString(integer);
        System.out.println("Binary value of " + integer + " is " 
                + binary + ".");
        
        int original = Integer.parseInt(binary, 2);
        System.out.println("Integer value of binary '" + binary 
                + "' is " + original + ".");
    }
    
}

Here is the result of our program.

Binary value of 127 is 1111111.
Integer value of binary '1111111' is 127.