2

Just started to learn programming could u explain whats difference between them

public class tst {

    public static void main(String args[]){
        int mk=1200;
        System.out.println(mk);
    }

}

public class tst {

    public static void main(String args[]){
        int mk=01200;
        System.out.println(mk);
    }
}

First one gave result 1200 while second one 640.

SomeJavaGuy
  • 7,237
  • 2
  • 19
  • 33

2 Answers2

4

A leading 0 makes the compiler parse the number as an octal number (radix 8). The decimal value of 01200 octal is 640.

Eran
  • 374,785
  • 51
  • 663
  • 734
2

A leading zero implies an octal literal; so 01200 is base 8 which, in decimal, is 640.

Essentially this is paying homage to older times where octal literals were much more common. These days though it's more likely to be a distraction and cause for confusion. There are movements to propose the more deliberate 0o notation for an octal literal.

Bathsheba
  • 227,678
  • 33
  • 352
  • 470