Define integer literals as octal values in Java



Literals with a leading zero are octal literals. Any number prefixed with a 0 is considered octal. Octal numbers can only use digits 0-7, just like decimal can use 0-9, and binary can use 0-1.

To define integer literals as octal value in Java is effortless. Here is the declaration and initialization.

 int myOct = 023; 

Example

Live Demo

 public class Demo { public static void main(String []args) { int myOct = 023; System.out.println(myOct); } } 

Output

 19 

Let us see another example.

Example

Live Demo

 public class Demo { public static void main(String []args) { int myOct = 010; System.out.println(myOct); } } 

Output

 8 
Updated on: 2019-07-30T22:30:23+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements