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型態的變數

image

接下來,Long型態 s5不能直接轉成 String,會報錯 ClassCastException

image

需要用 toString( )方法,才可以達成我們的需求

image

四.參考資料

Java中从Long到String的转换

arrow
arrow
    文章標籤
    Java OCA 1Z0-808 型態轉換
    全站熱搜

    書童 發表在 痞客邦 留言(0) 人氣()