java – 为什么“010”等于8?

我的简单问题是为什么:
System.out.println(010|4);

打印“12”?我理解按位OR运算符,但为什么“010”等于8?这绝对不是恭维2的通知,那么如何解码这个数字呢?

解决方法

看看 Java Language Specification,Chapter 3.10.1 Integer Literals

An integer literal may be expressed in decimal (base 10),hexadecimal
(base 16),octal (base 8),or binary (base 2).

[…]

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.

现在你应该明白为什么010是8.

相关文章

最近看了一下学习资料,感觉进制转换其实还是挺有意思的,尤...
/*HashSet 基本操作 * --set:元素是无序的,存入和取出顺序不...
/*list 基本操作 * * List a=new List(); * 增 * a.add(inde...
/* * 内部类 * */ 1 class OutClass{ 2 //定义外部类的成员变...
集合的操作Iterator、Collection、Set和HashSet关系Iterator...
接口中常量的修饰关键字:public,static,final(常量)函数...