Convert Decimal to Binary in Java

Java 8Object Oriented ProgrammingProgramming

Use Integer.toBinaryString() method to convert Decimal to Binary in Java.

Firstly, set a decimal value.

int val = 300;

Now use the Integer.toBinaryString() method to perform conversion on the above given decimal value.

String res = Integer.toBinaryString(val);

Let us see the complete example.


 Live Demo

public class Demo {
   public static void main( String args[] ) {
      int val = 300;
      System.out.println("Decimal = "+val);
      String res = Integer.toBinaryString(val);
      System.out.println("Binary = "+res);


Decimal = 300
Binary = 100101100
Updated on 26-Jun-2020 10:49:33