what's more efficient? to empty an object or create a new one?

前端 未结 6 840
北恋
北恋 2021-02-05 23:07

how expensive is \'new\'? I mean, should I aim at reusing the same object or if the object is \'out of scope\' it\'s the same as emptying it?

example, say a method crea

6条回答
  •  无人及你
    2021-02-05 23:40

    its an array list, so creating a new object means allocating a slab of memory and zeroing it, plus any bookkeeping overhead. Clearing the list means zeroing the memory. This view would lead you to believe that clearing an existing object is faster. But, it's likely that the JVM is optimized to make memory allocations fast, so probably none of this matters. So just write clear, readable code, and don't worry about it. This is java after all, not c.

提交回复
热议问题