SCJP題庫第110題
Given:
11. public static void test(Sting str){
12. int check = 4;
13. if(check = str.length()){
14. System.out.print(str.charAt(check -= 1) + ", ");
15. }else{
16. System.out.print(str.charAt(O) + ", ");
17. }
18. }
and the invocation:
21. test("four");
22. test("tee");
23. test("to");
What is the result?
A. r, t, t,
B. r, e, o,
C. Compilation fails.
D. An exception is thrown at runtime.
Ans: C
解說:
13. if (check = str.length()) è if (check == str.length()) {
在Java裏,所有條件式皆需是boolean型態,不能像在C/C++時代,以非零是true的觀念來看!
上式是因為check是整數型態,而check == str… 最終是boolean型態
Comments