先貼出一道最近去某公司的面試題,不知道出自哪裏?有哪位見多識廣的仁兄可以告知一聲,不勝感激!
public class test (
2. public static void main (String args[]) {
3. int i = 0xFFFFFFF1;
4. int j = ~i;
5.
6. }
7. )
What is the decimal value of j at line 5?
A. 0
B. 1
C. 14
D. –15
E. An error at line 3 causes compilation to fail.
F. An error at line 4 causes compilation to fail.
Answer: C
選項是英文給我的印象很深,讓我一下子穿越到了英語四級的考試現場,讓我趕緊畫一些關鍵字~~
大家是不是開始在草稿紙上算起來了,大家可以先猜猜是什麼答案!反正這道題我是選的E、F,題目要求也沒有要求是單選。。
結果,結果我就悲劇了,回來立馬運行了下面的程序,事實勝於雄辯!
class Test{
public static void main(String args[]){
int i=0xFFFFFFF1;
int j=~i;
int max = Integer.MAX_VALUE ;
int min = Integer.MIN_VALUE ;
System.out.println("int i value= " + i +
" hex value " + Integer.toHexString(i) );
System.out.println("int j value= " + j +
" hex value " + Integer.toHexString(j) );
System.out.println("Integer.MAX_VALUE= " +Integer.MAX_VALUE +
" hex value " + Integer.toHexString(Integer.MAX_VALUE ) );
System.out.println("Integer.MIN_VALUE= " + Integer.MIN_VALUE +
" hex value " + Integer.toHexString(Integer.MIN_VALUE ) );
}
}
輸出結果:
好了,看到輸出的那一刻,才發現我的答案應該是什麼了!
Tips:遇到拿不準的問題,最好的解決途徑就是上手調試,這樣你會馬上得到答案!