Clarification about “int” number that begins with 0

后端 未结 4 1191
难免孤独
难免孤独 2021-01-20 05:28
public class Test {

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

Why the output is : <

相关标签:
4条回答
  • 2021-01-20 05:37

    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

    0 讨论(0)
  • 2021-01-20 05:47

    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.

    0 讨论(0)
  • 2021-01-20 05:50

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

    0 讨论(0)
  • 2021-01-20 05:58

    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.

    0 讨论(0)
提交回复
热议问题