为什么带有前导零的整数文字会被奇怪地解释? [英] Why are integer literals with leading zeroes interpreted strangely?

查看:26
本文介绍了为什么带有前导零的整数文字会被奇怪地解释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这会打印 83

System.out.println(0123)

但是这会打印 123

System.out.println(123)

为什么会这样?

推荐答案

前导零表示文字使用 八进制(基数为 8 的数字).

A leading zero denotes that the literal is expressed using octal (a base-8 number).

0123 可以通过 (1 * 8 * 8) + (2 * 8) + (3) 进行转换,等于十进制的 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆