This is a Java Program to Illustrate Use of Binary Literals.A literal is an explicit value. To specify a binary literal, add the prefix 0b or 0B to the number. For example : int number=123;
The value 123 is a literal. Since it’s a normal base-10 value, we might say it is a decimal literal.
The value 123 is a literal. Since it’s a normal base-10 value, we might say it is a decimal literal.
We use binary literals with different type of datatypes like byte, short and int to illustrate its use. We also use addition operation to add two binary literals.
Here is the source code of the Java Program to Illustrate Use of Binary Literals. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
import java.util.Scanner;
public class Binary_Literal
{
public static void main(String[] args)
{
byte aB = 0b00100001;
short aS = 0b10100010100;
int a1 = 0b10110;
int a2 = 0b101;
int a3 = 0b1011;
int aI=a2+a3;
System.out.println("Byte value:"+aB);
System.out.println("Short value:"+aS);
System.out.println("Integer value:"+a1);
System.out.println("Result:"+aI);
}
}
Output:
$ javac Binary_Literal.java $ java Binary_Literal Byte value:33 Short value:1300 Integer value:22 Result:16
Sanfoundry Global Education & Learning Series – 1000 Java Programs.
advertisement
Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.
Related Posts:
- Check Programming Books
- Apply for Computer Science Internship
- Check Java Books
- Apply for Java Internship
- Practice Programming MCQs