Java Program to convert from decimal to binary

To convert decimal to binary, Java has a method Integer.toBinaryString(). The method returns a string representation of the integer argument as an unsigned integer in base 2.

Let us first declare and initialize an integer variable.

int dec = 25;

Convert it to binary.

String bin = Integer.toBinaryString(dec);

Now display the “bin” string, which consists of the Binary value. Here is the complete example.


 Live Demo

public class Demo {
   public static void main( String args[] ) {
      int dec = 25;
      // converting to binary and representing it in a string
      String bin = Integer.toBinaryString(dec);



Updated on: 18-Jun-2024

21K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started