返回使用最终原语的匿名类。它是如何运作的?
我想知道是否有人可以解释以下代码是如何工作的:
public interface Result {
public int getCount();
public List<Thing> getThings();
}
class SomeClass {
...
public Result getThingResult() {
final List<Thing> things = .. populated from something.
final int count = 5;
return new Result {
@Override
public int getCount() {
return count;
}
@Override
public List<Thing> getThings();
return things;
}
}
}
...
}
原始 int 、 List 引用和 List 实例存储在内存中的位置?它不能在堆栈上..那么在哪里呢? 在这种情况下,引用和原语的处理方式有区别吗?
非常感谢, 蒂姆·P。
I was wondering if someone could explain how the following code works:
public interface Result {
public int getCount();
public List<Thing> getThings();
}
class SomeClass {
...
public Result getThingResult() {
final List<Thing> things = .. populated from something.
final int count = 5;
return new Result {
@Override
public int getCount() {
return count;
}
@Override
public List<Thing> getThings();
return things;
}
}
}
...
}
Where do the primitive int , List reference and List instance get stored in memory? It can't be on the stack.. so where?
Is there a difference between how references and primitives are handled in this situation?
Thanks a bunch,
Tim P.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用的最终局部变量(以及任何外部
this
引用)在构造期间被复制到内部类的合成字段。与往常一样,引用和原语的处理方式相同。两者都被(浅)复制。您可以使用 JDK 中的 javap 来查看正在生成的内容。
The used final locals (and any outer
this
references) are copied to synthetic fields of the inner class during construction. References and primitives are, as always, treated the same. Both are (shallow) copied.You can use
javap
from the JDK to see what is being generated.