当前位置: 技术问答>java相关
请帮我分析一个垃圾收集的问题
来源: 互联网 发布时间:2017-04-13
本文导语: 1.class Test{ 2. public static void main(String[] args){ 3. String a=methodA(); 4. System.out.println(a); 5. // possibly more code... 6. } 8. pulic static String methodA(){ 9. String s="Study"; 10. String t="studY"; 11. ...
1.class Test{
2. public static void main(String[] args){
3. String a=methodA();
4. System.out.println(a);
5. // possibly more code...
6. }
8. pulic static String methodA(){
9. String s="Study";
10. String t="studY";
11. return s;
12. }
13.}
Two objects created in lines 9 and 10.
Which of these stements are true concerming these objects?
a. the object in line 9 is not eligible for collection at line 5
b. the object in line 10 is not eligible for collection at line 5
c. the object in line 9 is eligible for collection at line 5
d. the object in line 10 is eligible for collection at line 5
请帮我详细分析一下答案为什么选a.d?
2. public static void main(String[] args){
3. String a=methodA();
4. System.out.println(a);
5. // possibly more code...
6. }
8. pulic static String methodA(){
9. String s="Study";
10. String t="studY";
11. return s;
12. }
13.}
Two objects created in lines 9 and 10.
Which of these stements are true concerming these objects?
a. the object in line 9 is not eligible for collection at line 5
b. the object in line 10 is not eligible for collection at line 5
c. the object in line 9 is eligible for collection at line 5
d. the object in line 10 is eligible for collection at line 5
请帮我详细分析一下答案为什么选a.d?
|
答案是a,d没错!因为在methodA方法中的变量s和t是方法局部变量,这只在方法中有效,当方法返回时,这些局部变量就被垃圾收集了。局部变量t就是这种情况。而局部变量s是要作为方法的返回值返回的,所以这个变量还没有被垃圾收集。在调用方法methodA的时候,String类型变量a还是指向了原有在methodA中的局部变量s指向的内容"Study"。明白了吗?
|
String s's reference is returned by methodA() and assigned to a.
a is visible throughout main() and not eligible for collection.
when methodA() return, String t is absolutely useless, so during the execution of the lines after line 4 in main(), t is eligible for collection.
a is visible throughout main() and not eligible for collection.
when methodA() return, String t is absolutely useless, so during the execution of the lines after line 4 in main(), t is eligible for collection.
|
同意ervinlj(阳光男孩)
|
1楼和2楼的说的对
|
该结贴了把!
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。