本文介紹了Java int 部門讓我感到困惑的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
我正在做非常簡(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).
來自 第 3.10.1 節(jié)Java 語言規(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 部門讓我感到困惑的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!