有几个关于类型转换的比试题问问大家 最好能说说原理
1 Consider the following piece of code, 看下面的代码:
1 String s = "abcd";
2 Integer x = new Integer(3);
3 String s2 = s + 4;
4 s2 = null;
5 s = null;Following the execution of which line above, is the object reference by s2 available for garbage collection?(上面各行的执行,s2对象引用可以被垃圾收集么)A Line5
B It is not possible to say when an object is available for Garbage collection(说一个对象可以被垃圾收集是不可能的)
C Line4
D The object is available untile the executing thread is ended
(知道执行线程结束对象才可用)2 Which of the following will compile correctly?
A Short myshort = 99S;
B int t = "abc".length();
C float z = 1.0;
D char c =17c;3 下面代码输出结果是:
int i = 012;
int j = 034;
int k = (int)056L;
int l = 078;
System.out.println(i);
System.out.println(j);
System.out.println(k);
A 输出 12,34,56
B 输出 10,28,46
C int k = (int)056L;行编译错误
D int l = 078;行编译错误
1 Consider the following piece of code, 看下面的代码:
1 String s = "abcd";
2 Integer x = new Integer(3);
3 String s2 = s + 4;
4 s2 = null;
5 s = null;Following the execution of which line above, is the object reference by s2 available for garbage collection?(上面各行的执行,s2对象引用可以被垃圾收集么)A Line5
B It is not possible to say when an object is available for Garbage collection(说一个对象可以被垃圾收集是不可能的)
C Line4
D The object is available untile the executing thread is ended
(知道执行线程结束对象才可用)2 Which of the following will compile correctly?
A Short myshort = 99S;
B int t = "abc".length();
C float z = 1.0;
D char c =17c;3 下面代码输出结果是:
int i = 012;
int j = 034;
int k = (int)056L;
int l = 078;
System.out.println(i);
System.out.println(j);
System.out.println(k);
A 输出 12,34,56
B 输出 10,28,46
C int k = (int)056L;行编译错误
D int l = 078;行编译错误
Short myshort = 99S; 这种写法不对么??
float z = 1.0; 为什么也通不过?
float z = 1.0;改为float z=1.0f;就行了。系统默认浮点数是double型。
Short myshort = 99S; 这种写法不对 那该怎么写好呢?
两个都置空了
那么垃圾处理器 就可以把它原来分配的内存干掉了吧