为什么带有前导零的整数文字会被奇怪地解释? [英] Why are integer literals with leading zeroes interpreted strangely?
本文介绍了为什么带有前导零的整数文字会被奇怪地解释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这将打印83
System.out.println(0123)
但是这会打印123
System.out.println(123)
为什么这样工作?
推荐答案
前导零表示使用 0123,该值等于十进制的83. 由于某些原因,八进制浮点数不可用.
0123 can be converted by doing (1 * 8 * 8) + (2 * 8) + (3)
, which equals 83 in decimal.
For some reason, octal floats are not available.
如果您不希望文字以八进制表示,请不要使用前导零.
Just don't use the leading zero if you don't intend the literal to be expressed in octal.
还有一个0x
前缀,表示文字以十六进制(基数16)表示.
There is also a 0x
prefix which denotes that the literal is expressed in hexadecimal (base 16).
这篇关于为什么带有前导零的整数文字会被奇怪地解释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文