3
public class Test {

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

Why the output is : 10?

Maroun
  • 94,125
  • 30
  • 188
  • 241
Naveen Kocherla
  • 499
  • 7
  • 17

4 Answers4

14

If a number starts with 0 it's an octal number with base 8. 012 is in decimal a 10

Constantin
  • 8,721
  • 13
  • 75
  • 126
  • 1
    @user2642221 You're welcome - if this answer solved your problem, please accept it as answer to your question (by clicking on the checkmark). – Constantin Jan 29 '14 at 12:38
7

See the JLS:

An octal numeral consists of an ASCII digit 0 followed by one or more of the ASCII digits 0 through 7 interspersed with underscores, and can represent a positive, zero, or negative integer.

It's a good practice to write:

int i = 0_12;

It might be clearer now, i in decimal is 2*80 + 1*81 = 10.

Maroun
  • 94,125
  • 30
  • 188
  • 241
3

Octal Number : Any number start with 0 is considered as an octal number (012) i.e. base-8 number system

Simple octal number evaluation :

1*8^1 + 2*8^0 = 10

Octal Number

For More information about Number System

Kamlesh Arya
  • 4,864
  • 3
  • 21
  • 28
2

012 is the octal value for 10 in decimal. So your telling java to print the integer at octal pos 012. Here: http://www.asciitable.com/ shows octal to decimal vaulue conversions.

javawocky
  • 899
  • 2
  • 9
  • 31