为什么08在Java中不是有效的整数文字? [英] Why is 08 not a valid integer literal in Java?

查看:140
本文介绍了为什么08在Java中不是有效的整数文字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么 08 被视为超出范围int但 07 及以下不是?

Why is 08 considered an out of range int but 07 and below are not?

推荐答案

在Java和其他几种语言中,以 0 开头的整数文字被解释为八进制(基数8)数量。

In Java and several other languages, an integer literal beginning with 0 is interpreted as an octal (base 8) quantity.

对于一位数字数字( 08 09 ,这是不允许的),结果是一样的,所以你可能不会注意到它们被解释为八进制。但是,如果您使用多个有效数字编写数字,您可能会对结果感到困惑。

For single-digit numbers (other than 08 and 09, which are not allowed), the result is the same, so you might not notice that they are being interpreted as octal. However, if you write numbers with more than one significant digit you might be confused by the result.

例如:

010 ==  8
024 == 20

由于八进制文字通常不是您想要的,因此您应该始终注意永远不要使用 0 开始整数文字,除非您实际上是在尝试写零本身。

Since octal literals are usually not what you want, you should always take care to never begin an integer literal with 0, unless of course you are actually trying to write zero by itself.

这篇关于为什么08在Java中不是有效的整数文字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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