Tuesday, August 9, 2022

Octal numbers in Java

Literals with a leading zero are octal literals. 

Any number prefixed with a 0 is considered octal in Java(or in many programming languages for that matter). 

Octal numbers can only use digits 0-7, just like decimals can use 0-9, and binary can use 0-1.


Here is the declaration and initialization.

int myOct = 034;

Example

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

Output

28

Let's take another example.

Example 2:


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

Output

19
The output will always be printed in decimal. That is the reason why we get the decimal values as `28` and `19` in the output.
The octal of  `19`(decimal) is `023`(octal) and the octal of `28`(decimal) is `034'(octal)

No comments:

Post a Comment

Setting up the environment variables for Java in Windows

How to set the Environment Variables for Java in Windows? Step 1 -   Go to Control Panel -> System and Security  System .  In System, sel...