17.现在有如下一段程序:
class Happy {
public static void main(String args[]) {
float [][] f1 = {{1.2f,2.3f},{4.5f,5.6f}} ;
Object oo = f1 ;
f1[1] = oo ;
System.out.println("Best Wishes "+f1[1]);
}
}
运行该程序会出现什么结果?
A. {4.5,5.6}
B. 4.5
C. compilation error in line NO.5
D. exception
正确答案是:C
首先,oo是object类型,转换需要强制类型转换。此处编译失败
其次,就算是强转float[],运行也会报错,因为f1[1]是 float[] ,而oo是由 float [][] f1赋值得来的。
package com.songxinfeng;
public class Test {
public static void main(String args[]) {
float [][] f1 = {{1.2f,2.3f},{4.5f,5.6f}} ;
Object oo = f1 ;
f1[1] =((float[][]) oo)[1];
System.out.println("Best Wishes1 "+f1[1]);
System.out.println("-------------");
f1[1] = (float[]) oo ;
// f1[1] = oo ;
System.out.println("Best Wishes2 "+f1[1]);
}
}