Given: 11. public static void main(String[] args){ 12. String str = "null"; 13. if (str == null) { 14. System.out.println("null"); 15. }else (str.length() == 0) { 16. System.outprintln("zero"); 17. }else{ 18. System.out.println("some"); 19. } 20. } What is the result?[1] A. null B. zero C. some D. Compilation fails. E. An exception is thrown at runtime.
少了一個if.
[1] http://192.192.246.169/~wells/wiki/index.php/SCJP_1.6%E7%89%88%E8%80%83%E9%A1%8C_119
這不是重點啦XD
如果成是正確的, 有加上if呢?!
答案會是some, 因為String理面有字串"null", str.length() = 4.
除非你將String str="", 那答案才會是zero.
好像不會有答案跳到null, 因為str如果沒給値就放置判斷式中,
這個String沒初始化, 在編譯時就會被擋下來.
ex_119_string_null.java:4: error: variable s might not have been initialized if(str == null) ^ 1 error
- - - - -
public class ex_119_string_null{ public static void main(String args[]){ String str =""; if(str == null) System.out.println("null"); else if(str.length()==0) System.out.println("zero"); else System.out.println("else: "+str+" "+str.length()); } }
沒有留言:
張貼留言