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

查看:23
本文介绍了为什么 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.

>

对于个位数(除了不允许的0809),结果是一样的,所以你可能没有注意到它们被解释为八进制.但是,如果您编写的数字超过一位有效数字,您可能会对结果感到困惑.

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天全站免登陆