Java int除法让我困惑

标签 java int division octal

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

此代码按预期打印 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/

相关文章:

c - 除法和类次的区别

r - 两个整数相除,给出整数商和余数,R中不用除法或乘法

java - 使用poi API读取XLSX文件不起作用

java - 无法使用 Play 2.5 发送异步电子邮件

java - 为什么我的代码给出了这个数学问题的错误答案?

java - 用户输入的数字不相加

java - 计算器的简单除法不起作用

java - Powermock 和 Mockito。在模拟和 stub 同一个类时避免对类进行静态初始化

java - 递归扫描 SolrJ 文件夹中的文档以建立索引

在 C 中将 char 数组转换为整数数组