English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
To convert decimal to binary, Java has a method 'Integer.toBinaryString()'. This method returns the string representation of an integer parameter as a base2Unsigned integer with base 10.
Let's first declare and initialize an integer variable.
int dec = 25;
Convert it to binary.
String bin = Integer.toBinaryString(dec);
Now display the 'bin' string composed of binary values. This is a complete example.
public class Demo { public static void main(String args[]) { int dec = 25; //Convert to binary and represent it as a string String bin = Integer.toBinaryString(dec); System.out.println(bin); } }
Output Result
11001