提取大量的字符串文字是一个好主意吗?(Is extracting a large number of String literals a good idea? [closed])

系统教程 行业动态 更新时间:2024-06-14 16:57:17
提取大量的字符串文字是一个好主意吗?(Is extracting a large number of String literals a good idea? [closed])

在我正在处理的遗留代码库中,有大量的字符串文字。 其中大量是重复的。 例如,在500个地方使用字符串“userID”。 可能有一千种这样的文字以重复的方式使用。 IntelliJ Idea静态代码分析表明,我将这些提取为常量。 如果IDE为我自动执行重构,没有我输入一行代码,我应该去做吗?

一般来说,提取许多重复的字符串文字作为常量是否是个好主意? 这显然会避免重复,将提供单点访问,声明等。

但是,这些文字中的一些在访问时会出现。 如果我将所有文字声明为常量(静态最终),那么所有这些都将被加载在一起。 在这种情况下,将所有这些文字声明为常量是一个好主意吗? 你能提供一些垃圾收集的指针,在这种情况下的内存空间预防措施? 这种情况下使用的最佳做法是什么?

一些注意事项:我知道字符串文字被实习。 所以我不会在最糟糕的情况下保存任何内存。 此外,似乎jdk 7会将这些字符串放在堆中而不是permgen中。 我看到了像我这样的几个 问题 ,但觉得它是不同的。 所以张贴在这里。

谢谢

In a legacy code base I am dealing with, there are vast number of String literals. A large number of them are duplicates. For example, the string "userID" is used say in 500 places. There are maybe a thousand such literals which are used in a repeated manner. IntelliJ Idea static code analysis suggests that I extract those as constants. If the IDE does this refactoring automatically for me, without me typing a single line of code, should I go for it?

Is it a good idea, in general, to extract many such duplicate string literals as constants? This will obviously avoid duplication, will provide single point of access, declaration, etc.

However, some of these literals come into picture when accessed. If I declare all literals as constants (static final), then all those will be loaded together. In that context, is it a good idea to declare all those literals as constants? Could you provide some pointers to garbage collection, memory space precautions in such scenarios? What are best practices used in such scenario?

Some notes: I know that string literals are interned. So I don't thing I would be saving any memory in the worst case. Also it seems that jdk 7 will put those strings in heap rather that permgen. I saw a couple of questions like mine, but feel that it is different. So posting it here.

Thanks

最满意答案

所有字符串文字都是自动实现的。 从JDK7 +开始,当定义它们的类(实际上是加载定义字符串文字的类类加载器 )得到GCed时(如果没有其他类引用它(虽然很少发生这种情况)),它们将被GCed。 final并将它们放到一个普通的类中,从内存节约的角度来看确实没有用处,但从设计的角度来看是有用的,因为它将提供单一的访问点。

JVM中的所有类共享相同的字符串文字。 所以,不会有新的字符串。 实际上,将它们放入一个类并从该位置访问它们会使您的代码更具结构化和更具可读性。

我的建议是,不要修改遗留代码,除非它有很大的不同。 权衡是你的选择。 :P

All String Literals are interned automatically. From JDK7+, they will be GCed when the class (actually the classloader which loaded the class which defines the string literal) which defines them gets GCed (provided no other class refers to it (though this happens rarely..). Making them static and final and putting them into a common class is indeed useless from a Memory saving perspective but useful from a design perspective because it will provide a single point of access.

The same String literals are shared across all classes in the JVM. So, there will be no new Strings. Effectively, putting them into one class and accessing them from that place makes your code more structured/ more readable.

My suggestion, don't tinker with legacy code unless it makes a lot of difference. The trade-offs are yours' to choose. :P

更多推荐

本文发布于:2023-04-12 20:06:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/32ede7aa55346d512a555a59a8313d4c.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:是一个   字符串   好主意   文字   extracting

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!