我正在做非常简单的整数除法,但得到的结果很奇怪。
此代码按预期打印 2
:
public static void main(String[] args) {
int i = 200;
int hundNum = i / 100;
System.out.println(hundNum);
}
此代码打印 1
为 not expected:
public static void main(String[] args) {
int i = 0200;
int hundNum = i / 100;
System.out.println(hundNum);
}
这是怎么回事?
(Windows XP Pro,Java 1.6 在 Eclipse 3.4.1 中运行)
最佳答案
值 0200
是一个八进制(以 8 为基数)常量。它等于 128(十进制)。
来自 Section 3.10.1 of the Java Language Specification :
An octal numeral consists of an ASCII digit 0 followed by one or more of the ASCII digits 0 through 7 and can represent a positive, zero, or negative integer.
关于Java int除法让我困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1705645/