close
一.題目
Given the code fragment:
What is the result?
A. Sum is 600
B. Compilation fails at line n1.
C. Compilation fails at line n2.
D. A ClassCastException is thrown at line n1.
E. A ClassCastException is thrown at line n2.
二.答案
C
三.解析
line n1的Short可以強轉成 long,然後 Integer型態的s2也會自動轉型成 long,再相加。
line n2的 s3 * s2,Long型態的 s3無法直接乘 Integer的s2,需要兩者相乘後,賦值給一個 Long型態的變數
接下來,Long型態 s5不能直接轉成 String,會報錯 ClassCastException
需要用 toString( )方法,才可以達成我們的需求
四.參考資料
Java中从Long到String的转换
文章標籤
全站熱搜
留言列表