gpt4 book ai didi

Java int除法让我困惑

转载 作者:搜寻专家 更新时间:2023-10-30 21:39:23 25 4
gpt4 key购买 nike

我正在做非常简单的整数除法,但得到的结果很奇怪。

此代码按预期打印 2:

public static void main(String[] args) {
int i = 200;
int hundNum = i / 100;
System.out.println(hundNum);
}

此代码打印 1not 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com