In this example you will learn how to convert decimal number to binary number. To convert decimal number to binary we can use `Integer.toBinaryString()`

method. This method takes a single parameter of integer and return a string that represent the equal binary number.

If you want to convert the other way around, from binary string to decimal, you can use the `Integer.parseInt()`

method. This method takes two parameters. First, the string that represents a binary number to be parsed. The second parameter is the radix to be used while parsing, in case for binary number the radix is `2`

.

```
package org.kodejava.example.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.
```

### Wayan Saryada

Founder at Kode Java Org

I am a programmer, a runner, a recreational diver, currently live in the island of Bali, Indonesia. Mostly programming in Java, Spring Framework, Hibernate / JPA. If these posts help, you can support me, buy me a cup of coffee or tea. Thank you 🥳

#### Latest posts by Wayan Saryada (see all)

- How do I set the time of java.util.Date instance to 00:00:00? - October 24, 2019
- How to Install Consolas Font in Mac OS X? - March 29, 2019
- How do I clear the current command line in terminal? - February 14, 2019

Thanks for sharing this nice post about how to convert decimal number to binary number in Java. Keep posting.

Best place to learn java programs. Thanks for sharing.

https://www.flowerbrackets.com/java-decimal-to-binary-using-tobinarystring-and-stack/