为什么不能从零开始? [英] Why can't a long start with zero?

查看:135
本文介绍了为什么不能从零开始?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

在识别9的地方不被认可09

想知道,当我声明以下内容时:

Just wondering, when I declare the following:

public static final long __VERSIONCODE = 0L;
public static final long __VERSIONCODE = 9L;

这将起作用,但是每当我尝试以下操作时:

that will work, but whenever I try this:

public static final long __VERSIONCODE = 09L;
public static final long __VERSIONCODE = 08235L;

我遇到错误(日食):


long类型的文字09L超出范围。

"The literal 09L of type long is out of range."

所以我认为是因为它从零开始。

So I thought that was because it started with a zero.

,但随后我尝试将第二位数字降低为8:

but then I tried with the second digit lower as eight:

public static final long __VERSIONCODE = 07235L;

这没有错误。

然后:

public static final long __VERSIONCODE = 07239L;

也给我一个错误。

所以我真的不明白我可以分配给long的值和我不能分配的值。为什么会出现此错误? (实际上只是出于好奇,我也可以只使用String作为版本代码)。

So I really don't get the point of which values I can assign to a long and which I can't. Why do I get this errors? (It's actually just that I'm curious, I can also just use a String for my version code).

此外,我忘了提到使用双精度字符而不是长整数来表现完全相同。

Also, I forgot to mention that this behaves exactly the same using doubles instead of longs.

推荐答案

在整数类型的文字前面加上0时,它将解释为表示八进制数字。由于 9不是八进制数字的有效数字,因此可能就是这种情况。尝试打印出(十进制)值 010L,然后查看是否显示 8以确认。

When you put a 0 in front of an integer-type literal, it will interpret it as representing an octal number. Since "9" isn't a valid digit for octal numbers, that might be what's going on. Try printing out the (decimal) value of "010L" and see whether is says "8" to confirm.

注意:不确定Java是否执行此操作或是否执行此操作纯粹是Eclipse的产物。如果是后者,则打印出010L将显示10。如果是前者,您将看到8。如果它只是Eclipse的工件,则可以尝试01L,02L,...,07L来确认,这一切都应该起作用,并且08L和09L会失败。

Note: not sure if Java does this, or if this is purely an artifact of Eclipse. If the latter, printing out 010L would show 10. If the former, you'd see 8. If it's just an artifact of Eclipse, you can confirm by trying 01L, 02L, ..., 07L, which should all work, and 08L and 09L which would fail.

这篇关于为什么不能从零开始?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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