Tuesday 7 May 2013

How to Use Octal Values in Java

You can tell Java that a number is in octal (base 8) by prefixing it with a zero.

In the example below, the variable octal_77 is assigned the value 077 i.e. octal 77. The program then displays the value in decimal (base 10).

UNIX > cat prog21.java
public class prog21
{
public static void main (String args[])
  {
  int octal_77 = 077;
  System.out.println
  ("octal 77 = " + octal_77);
  }
}
UNIX > javac prog21.java
UNIX > java prog21
octal 77 = 63
UNIX > 

You can check the result yourself by converting octal 77 to decimal (base 10) like this:

octal 77 = (in decimal or base 10) (7 x 8) + 7 = 63

If you have a Java book on Amazon, which you would like to advertise here for free, please write to me at international_dba@yahoo.co.uk.