問(wèn)題描述
我正在做非常簡(jiǎn)單的 int 除法,結(jié)果很奇怪.
I am doing very simple int division and I am getting odd results.
此代碼按預(yù)期打印 2
:
public static void main(String[] args) {
int i = 200;
int hundNum = i / 100;
System.out.println(hundNum);
}
此代碼將 1
打印為 not 預(yù)期:
This code prints 1
as 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 中運(yùn)行)
推薦答案
值 0200
是一個(gè) 八進(jìn)制 (base 8) 常量.它等于 128(十進(jìn)制).
The value 0200
is an octal (base 8) constant. It is equal to 128 (decimal).
來(lái)自 第 3.10.1 節(jié)Java 語(yǔ)言規(guī)范:
八進(jìn)制數(shù)字由一個(gè) ASCII 數(shù)字 0 后跟一個(gè)或多個(gè) ASCII 數(shù)字 0 到 7 組成,可以表示正整數(shù)、零整數(shù)或負(fù)整數(shù).
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.
這篇關(guān)于Java int 部門(mén)讓我感到困惑的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!