Define integer literals as octal values in Java

Java 8Object Oriented ProgrammingProgramming

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
raja
Published on 05-Dec-2018 11:41:01
Advertisements