是Java“缓存”吗?匿名课程? [英] Is Java "caching" anonymous classes?

查看:58
本文介绍了是Java“缓存”吗?匿名课程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下代码:

for(int i = 0;i < 200;i++)
{
  ArrayList<Integer> currentList = new ArrayList<Integer>() {{
    add(i);
  }};
  // do something with currentList
}




  • Java如何处理 currentList 的类?

  • 对于200个对象中的每一个,它会认为它是不同的类吗?

  • 即使创建了第一个对象,它是否会受到性能影响?

  • 是否以某种方式缓存它?

    • How will Java treat the class of currentList?
    • Will it consider it a different class for each of the 200 objects?
    • Will it be a performance hit even after the first object is created?
    • Is it caching it somehow?
    • 我只是好奇:)

      推荐答案

      ArrayList<Integer> currentList = new ArrayList<Integer>() {{
          add(i);
        }};
      

      每次通过循环创建匿名类的新实例 ,每次都没有重新定义或重新加载课程。该类定义一次(在编译时),并加载一次(在运行时)。

      is creating a new instance of the anonymous class each time through your loop, it's not redefining or reloading the class every time. The class is defined once (at compile time), and loaded once (at runtime).

      使用匿名类没有显着的性能损失。

      There is no significant performance hit from using anonymous classes.

      这篇关于是Java“缓存”吗?匿名课程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆