# How to convert Java Decimal to Binary

In Java, Decimal value can be converted into Binary value either by using Integer.toBinaryString() method or own custom code. Let's see the examples.

## 1. `Integer.toBinaryString()` Method

The toBinaryString() method is a part of Integer class which converts the decimal into binary strings which further can be converted into other types like `int` or `float`.

## Example 1:

Here, the decimal values are converted into binary strings by using the `toBinaryString()` method. See the example below.

``````public class StudyTonight
{
public static void main(String args[])
{
int d1 = 7;
int d2 = -232;
int d3 = 90902;

String b1 = Integer.toBinaryString(d1);
String b2 = Integer.toBinaryString(d2);
String b3 = Integer.toBinaryString(d3);

System.out.println("Binary value is : " +b1);
System.out.println("Binary value is : " +b2);
System.out.println("Binary value is : " +b3);
}
}``````

Binary value is : 111
Binary value is : 11111111111111111111111100011000
Binary value is : 10110001100010110

## Example 2:

The decimal values can be converted into binary using custom logic. This is the case when you don't want to use any built-in method.

``````public class StudyTonight
{
public static void main(String [] args)
{
int dec = 343;
int bin[] = new int;
int i = 0;
while(dec > 0)
{
bin[i++] = dec%2;
dec = dec/2;
}
for(int j = i-1; j >= 0 ;j--)
{
System.out.print(bin[j]);
}
System.out.println();
}
}``````

101010111