Make sure to subscribe to our newsletter and be the first to know the news.
Make sure to subscribe to our newsletter and be the first to know the news.
1) Class Declaration: The code starts with the declaration of a Java class named BinaryToDecimal.
2) Main Method: Inside the class, there's a main method which serves as the entry point for the Java application.
3) Binary String: A string variable named binary is declared and initialized with the binary value "110011". This represents a binary number that we want to convert to decimal.
4) Conversion to Decimal: The parseInt method of the Integer class is used to convert the binary string (binary) to its equivalent decimal representation. The second argument 2 specifies that the input string is in base-2 (binary). The result of this conversion is stored in an integer variable i.
5) Output: Finally, the code prints out both the binary and decimal representations of the number using System.out.println. It prints "Binary:110011 || Decimal:51".
public class BinaryToDecimal {
public static void main(String[] args) {
String binary="110011";
int i=Integer.parseInt(binary, 2);
System.out.println("Binary:"+binary+" || Decimal:"+i);
}
}