In  this tutorial, we will see “How to convert Decimal number to Binary using Java 8?”

How to convert decimal number to binary in java 8

Java 8 – How to convert Decimal number to Binary? Click To Tweet

 

Output:

How to convert decimal number to binary in java 8

In the above example, It uses IntStream and using it’s iterate method to repeatedly divide the decimal number by number 2, until the result of the division becomes 0. Then, the limit method is used to limit the number of iterations to 32, which is basically the number of bits in an int. After that, map method is used to get the remainder of each division, which is basically the binary digit at that place. After applying map method, now it comes to apply the mapToObj method to convert each binary digit to a string, and finally, it’s the reduce method which is used to concatenate all the binary digits into a single string. And, Then using sysout method, we are printing the resulting binary string to the console.

How to convert decimal number to binary in java 8

Java 8 – How to convert Decimal number to Binary? Click To Tweet

Do you like this Post? – then check my other helpful posts:

Other Useful References: