Java - int octal number format

int octal number format

If an integer literal starts with a zero and has at least two digits, it is considered to be in the octal number format.

The number 0 is treated as zero in decimal number format, and 00 is treated as zero in octal number format.

The following line of code assigns a decimal value of 17 (021 in octal) to num1:

  
// 021 is in octal number format, not in decimal 
int num1 = 021; 
  

The following two lines of code have the same effect of assigning a value of 17 to the variable num1:


int num1 = 17;   // No leading zero - decimal number format 
int num2 = 021;  // Leading zero - octal number format. 021 in octal is the same as 17 in decimal 

Demo

public class Main {
  public static void main(String[] args) {

    // No leading zero - decimal number format
    int num1 = 17;

    // Leading zero - octal number format. 021 in octal is the same as 17 in decimal
    int num2 = 021;

    System.out.println(num1);/*from ww w  .  jav  a  2s .  c om*/

    System.out.println(num2);
  }
}

Result

zero in decimal number format and zero in octal number format

int num1 = 0; // Assigns zero to num1, 0 is in the decimal number format 
int num2 = 00; // Assigns zero to num1, 00 is in the octal number format 

Demo

public class Main {
  public static void main(String[] args) {
    int num1 = 0; // Assigns zero to num1, 0 is in the decimal number format
    int num2 = 00; // Assigns zero to num1, 00 is in the octal number format

    System.out.println(num1);/*from   w w w .j  a v  a 2  s .c  o  m*/

    System.out.println(num2);
  }
}

Result