java PMD:避免在循环内实例化新对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17340421/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 01:43:20  来源:igfitidea点击:

PMD: Avoid instantiating new objects inside loops

javaloopsinstantiationpmd

提问by brimborium

I've got an issue with the PMD rule Avoid instantiating new objects inside loops. Here is some example code:

我对 PMD 规则有疑问Avoid instantiating new objects inside loops。下面是一些示例代码:

import java.awt.Dimension;

public class PMDDemo {
    public static void main(final String[] args) {
        final Dimension[] arr = new Dimension[10];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = new Dimension(i, i); // rule violation here
        }
    }
}

PMD gives me the above mentioned rule violation at the marked spot in the code. How am I supposed to create ninstances of a class without creating them within a loop?

PMD 在代码中的标记点给了我上面提到的规则违规。我应该如何创建一个类的n 个实例而不在循环中创建它们?

I know that some of PMD's rules are controversial (like the onlyOneExitrule). But up to now I at least understood the idea behind them. I don't understand the reasoning behind this rule. Can someone help me with that?

我知道 PMD 的一些规则是有争议的(比如onlyOneExit规则)。但到目前为止,我至少了解了它们背后的想法。我不明白这条规则背后的原因。有人可以帮我吗?

回答by assylias

For your specific use case it makes no sense as you keep the reference to the new Object afterthe loop. So there is no real alternative to your solution.

对于您的特定用例,在循环保留对新对象的引用是没有意义的。因此,您的解决方案没有真正的替代方案。

More generally speaking, creating short lived objects in Java is cheap* (apart from the hidden cost that the GC will run more often). In particular, the allocation is almost free and the time of GC mostly depends on the quantity of reachable objects - dead objects do not increase GC time for typical GC algorithms.

更一般地说,在 Java 中创建短期对象很便宜*(除了 GC 将更频繁地运行的隐藏成本)。特别是,分配几乎是免费的,GC 的时间主要取决于可达对象的数量——对于典型的 GC 算法,死对象不会增加 GC 时间。

The JIT can also perform various optimisations if it detects that unnecessary objects are created.

如果 JIT 检测到创建了不必要的对象,它也可以执行各种优化。

Obviously, creating useless is not a recommended practice, but trying to reuse objects is often counterproductive.

显然,创建无用的做法不是推荐的做法,但尝试重用对象通常会适得其反。

As a practical example, you can have a look at this postwhich shows that creating a new set within a loop is cheaper than creating one before the loop and clearing it at each iteration.

作为一个实际的例子,你可以看看这篇文章,它表明在循环中创建一个新集合比在循环之前创建一个新集合并在每次迭代时清除它便宜。

* Thanks @RichardTingle for the link

* 感谢@RichardTingle 提供链接

回答by Vishnu Ranganathan

for (int i = 0; i < arr.length; i++) {
  arr[i] = new Dimension(i, i); // rule violation here
}

The Above Pmd can be resolved by

上述 Pmd 可以通过以下方式解决

 for (int i = 0; i < arr.length; i++) {
   arr[i] = createNewDimension(i,i); // rule violation here
 }

 private static Dimension createNewDimension(i,i) {
   return new Dimension(i, i);
 }

we should not directly use new operator inside a loop just move this inside a private method.

我们不应该在循环中直接使用 new 操作符,而只是将它移动到私有方法中。