What is literal in Java?

What is literal in Java?

Literals are the constant values that can be assigned to a variable.

Types of literals:

  • Decimal Literal
  • Octal Literal

int x = 060;        

System.out.println(x);

When you use 0 as a prefix in Java, then Java treats this value as an octal value.

So, the output of the above code will be 48.

But when you write code like this

int x = 078;        

then this will give an error (the literal of type int is out of range) because the range of octal value is 0-7.

  • Hexadecimal Literal

int y = 0x45b;        
System.out.println("y is "+y);        

This line of code will print y is 1115.

In hexadecimal literal, Java is not case-sensitive.

So int y = 0x45b; and 0X45B; both are allowed.

Range of hexadecimal literal is 0-9 and a-f or A-F

  • Binary Literal

When we use 0b as a prefix then the value becomes binary literal.

int g = 0b1010;        
System.out.println("g is "+g);        

The output will be 10;

The range of binary literal is 0-1.






Prof. Dr. Manoj Sathe

Associate Professor - Computer Science

4 个月

Keep more practice Devidas

要查看或添加评论,请登录

Devidas Sabale的更多文章

  • All About Array

    All About Array

    What is Array: Array is group of primitive data, group of objects. Array is group of similar data (homogeneous data).

    1 条评论
  • Data Types in Java

    Data Types in Java

    Java is a strongly typed programming language. Therefore, you have to mention the type of value before compilation…

  • Var arg method in Java

    Var arg method in Java

    Prior to learning this concept, I used to think that we could not change the syntax of the main method. But we can make…

  • Why Java is not a fully object oriented programming language?

    Why Java is not a fully object oriented programming language?

    Here are reasons: Primitive Data Types: In Object Oriented Programming everything should be object. But in Java there…

社区洞察

其他会员也浏览了