Java 在重新创建对象之前是否需要将ArrayList置为空?
我不确定在使用arraylist时它是否会消耗更多的内存。在阅读以下代码块时,我感到困惑:Java 在重新创建对象之前是否需要将ArrayList置为空?,java,object,memory,Java,Object,Memory,我不确定在使用arraylist时它是否会消耗更多的内存。在阅读以下代码块时,我感到困惑: headerRow = new ArrayList<>(); headerRow.add(""); xlHeader.add(headerRow); // headerRow = null; //<----- This is the line of confusion. headerRow = new ArrayList<>();
headerRow = new ArrayList<>();
headerRow.add("");
xlHeader.add(headerRow);
// headerRow = null; //<----- This is the line of confusion.
headerRow = new ArrayList<>();
headerRow=newarraylist();
标题加上(“”);
xlHeader.add(headerRow);
//headerRow=null// 这不是必需的,它将引用新创建的对象。变量headerRow
将引用新创建的ArrayList
因此您可以直接使用headerRow=newarraylist()代码>这不是必需的,它将引用新创建的对象。变量headerRow
将引用新创建的ArrayList
因此您可以直接使用headerRow=newarraylist()
headerRow
将引用新创建的阵列列表
,旧的将注册到垃圾收集
因此,不需要取消
而且
是用于实例化的正确语法。headerRow
将引用新创建的ArrayList
,旧的将注册到垃圾收集
因此,不需要取消
而且
是用于实例化的正确语法。您只需使用
headerRow = new ArrayList();
无需在之前将其置空,JVM管理它
在前两行代码中
List<String> headerRow = new ArrayList<String>();
headerRow = new ArrayList();
List headerRow=new ArrayList();
headerRow=新的ArrayList();
第二行是多余的,不需要写headerRow=newarraylist()代码>
因为您已经在before行中进行了初始化。您只需使用
headerRow = new ArrayList();
无需在之前将其置空,JVM管理它
在前两行代码中
List<String> headerRow = new ArrayList<String>();
headerRow = new ArrayList();
List headerRow=new ArrayList();
headerRow=新的ArrayList();
第二行是多余的,不需要写headerRow=newarraylist()代码>
因为您已经在前一行中初始化了。我不知道您为什么这样做:
List<String> headerRow = new ArrayList<String>();
// First one is correct; lose the line that follows.
headerRow = new ArrayList();
List headerRow=new ArrayList();
//第一个是正确的;丢掉后面的线。
headerRow=新的ArrayList();
我不知道您为什么这样做:
List<String> headerRow = new ArrayList<String>();
// First one is correct; lose the line that follows.
headerRow = new ArrayList();
List headerRow=new ArrayList();
//第一个是正确的;丢掉后面的线。
headerRow=新的ArrayList();
此操作不会对性能产生任何影响
当我们以控件的形式使用null以不再执行操作时,可以将null设置为reference
比如说
Closable stream = getStream();
try {
stream.close();
stream = null;
catch(Exception e) {
log(e);
} finally {
if(stream != null) {
try { stream.close(); } catch(Exception empty) {}
}
}
此操作不会对性能产生任何影响
当我们以控件的形式使用null以不再执行操作时,可以将null设置为reference
比如说
Closable stream = getStream();
try {
stream.close();
stream = null;
catch(Exception e) {
log(e);
} finally {
if(stream != null) {
try { stream.close(); } catch(Exception empty) {}
}
}
在这种特殊情况下,它可能符合GC条件,因为xlHeader.add(headerRow)代码>。并且xlHeader是可访问的。在这种特殊情况下,它可能符合GC的条件,因为xlHeader.add(headerRow)代码>。并且可以访问xlHeader。