为什么09“太大”?整数? [英] Why is 09 "too large" of an integer number?

查看:195
本文介绍了为什么09“太大”?整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

他们认为:


可能重复:

带前导零的整数


但是如果你查看整数与前导零,你会发现问题是在jdk7发布之前是否被问及因此研究工作较少。但是在jdk7中,整数有一些变化和补充。以下是jdk7的最新答案。

But if you check Integer with leading zeroes then you will find that the question is asked if before the launch of jdk7 and therefore it has lower researching efforts. But in jdk7 there is some change and addition to the integers. Here are the answers which are up to date covering jdk7.

我有一个代码:

class Test{
    public static void main(String[] args){
        int x=09;
        System.out.println(x);
    }
}

编译时出错:整数过大:09

On compilation it gives an error: integer number too large : 09

为什么会这样做?

再次,如果我将代码更改为:

Again, if I change the code to:

class Test{
    public static void main(String[] args){
        int x=012;
        System.out.println(x);
    }
}

现在输出为10

为什么输出10而不是12?

Why it give the output 10 instead of 12?

推荐答案

以<$ c开头的数字$ c> 0 被视为八进制 - 而9不是八进制数字(但是(传统上) )0-7是)。

Numbers beginning with 0 are considered octal – and 9 is not an octal digit (but (conventionally) 0-7 are).

十六进制文字以 0x 开头,例如 0xA

Hexadecimal literals begin with 0x, e.g. 0xA.

直到Java 6,没有文字二进制符号和
表示法你必须使用类似

Up until Java 6, there was no literal notation for binary and you'll had to use something like

int a = Integer.parseInt("1011011", 2);

其中第二个参数指定所需的基数。

where the second argument specifies the desired base.

Java 7现在有二进制文字

Java 7 now has binary literals.


在Java SE 7中,整数类型(byte,short,int和long)可以也可以使用二进制数系统表示。要指定二进制文字,请在数字中添加前缀 0b 0B

这篇关于为什么09“太大”?整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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