Java - 在for循环中声明变量

发布于 2024-10-08 08:08:44 字数 291 浏览 9 评论 0原文

在循环内声明变量是不好的做法吗?在我看来,这样做(如下面的第一个代码块所示)将使用第二个代码块十倍的内存......由于在循环的每次迭代中创建一个新字符串。这是正确的吗?

for (int i = 0; i < 10; i++) {
  String str = "Some string";
}

String str;
for (int i = 0; i < 10; i++) {
  str = "Some String";
}

Is declaring a variable inside of a loop poor practice? It would seem to me that doing so, as seen in the first code block below, would use ten times the memory as the second... due to creating a new string in each iteration of the loop. Is this correct?

for (int i = 0; i < 10; i++) {
  String str = "Some string";
}

vs.

String str;
for (int i = 0; i < 10; i++) {
  str = "Some String";
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

落花随流水 2024-10-15 08:08:44

在循环内声明变量是不好的做法吗?

一点也不!它将变量本地化到其使用点。

在我看来,这样做(如下面的第一个代码块所示)将使用第二个代码块十倍的内存

。编译器可能会进行优化以保持内存使用效率。仅供参考:如果您使用 final 关键字告诉它您的变量具有对对象的固定引用,您可以帮助它。

注意:如果您有一个更复杂的对象,在构造函数中执行复杂的代码,那么您可能需要担心单次执行与多次执行,并在循环之外声明该对象。

Is declaring a variable inside of a loop poor practice?

Not at all! It localizes the variable to its point-of-use.

It would seem to me that doing so, as seen in the first code block below, would use ten times the memory as the second

The compiler may optimize things to keep memory use efficient. FYI: you can help it, if you use the final keyword to tell it that your variable has a fixed reference to an object.

Note: if you have a more complex object where you are executing complex code in the constructor, then you may need to worry about single vs. multiple executions, and declare the object outside of the loop.

倾`听者〃 2024-10-15 08:08:44

在这两个示例中,您将实例化一个新的字符串对象,其中包含相同次数的字符串“Some String”。

在第一个示例中,您在循环内声明 str ,在 for 循环完成后,对该字符串的所有引用都将丢失,从而允许 Java 的垃圾收集器从内存中删除该字符串的所有实例。但是,在第二个示例中,您在循环外部声明 str,您创建的最后一个字符串仍将在循环外部引用它,并且 Java 的垃圾收集器只会删除十分之九的字符串。实例化的内存中的字符串。

因此,第一种方法更好,因为您不保留字符串的任何引用,从而干扰垃圾收集器确定它是否仍在使用的能力。

In both examples, you're going to instantiate a new string object that contains the string "Some String" the same number of times.

In the first example where you declare str inside of the loop, all references to that string are going to be lost after the for-loop completes, allowing Java's garbage collector to remove all instances of the strings from memory. However, in the second example where you declare str outside of the loop, the last string you created will still have a reference to it outside the loop, and Java's garbage collector will only remove 9 out of 10 of the strings from memory that were instantiated.

As such, the first method is better since you do not hold onto any references of the string, interfering with the garbage collector's ability to determine if it's still in use.

再浓的妆也掩不了殇 2024-10-15 08:08:44

除了 @Jason S 所说的之外,我还鼓励您考虑代码的可读性。

例如,如果您只向引用写入一次,那么使用以下内容将使您的意图更加清晰:

String str = "write once";
while(condition){
    //do stuff with str
}

对比:

String str = null;
while(condition){
    str = "write once";
    //do stuff with str
}

同样,如果字符串的值确实 > 基于特定于循环迭代的内容,在循环内声明变量。

Beyond what @Jason S said, I'd also encourage you to think about the readability of the code.

For instance, if you are only writing to a reference once, it would make your intent more clear to use something like:

String str = "write once";
while(condition){
    //do stuff with str
}

Versus:

String str = null;
while(condition){
    str = "write once";
    //do stuff with str
}

Likewise, if the value of the string is really based off something that is specific to an iteration of the loop, declare the variable inside the loop.

壹場煙雨 2024-10-15 08:08:44

变量引用占用的内存很小。通常更好的做法是在循环内声明该项目,因为它将更接近使用它的位置并且更具可读性。

The memory used by a variable reference is small. It is typically better practice to declare the item inside the loop because it will be closer to where it is used and more readable.

望笑 2024-10-15 08:08:44

这取决于。效率低下的原因是创建 String 对象的开销,假设您的编译器没有更改任何内容。一旦超出范围,内存就会被清除。

It depends. The inefficiency is from the overhead creating the String object, assuming your compiler doesn't change anything. The memory will be cleared once it goes out of scope.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文