java中常用的三目運算符惑畴,大家都知道
int a = 10;
char b = 'x';
System.out.println(false?10:b);//結(jié)果為x
System.out.println(false?a:b);//結(jié)果為120
可是在看IO流源碼時發(fā)現(xiàn)了這個:
public synchronized int read() {
return (pos < count) ? (buf[pos++] & 0xff) : -1;
}
關于類型轉(zhuǎn)換的時間就成了問題蛋欣。
byte a = -1;
int b;
int e;
b = (true)?(a&0xff):-1;// b = 255
e = (true)?a:-1; // e = -1
我很奇怪a被轉(zhuǎn)型(int)的時間