什么是字符串实习在Java,当我应该使用它,为什么?
当前回答
http://docs.oracle.com/javase/7/docs/api/java/lang/String.html实习生()
基本上,对一系列字符串执行String.intern()将确保具有相同内容的所有字符串共享相同的内存。因此,如果你有一个名字列表,其中“john”出现了1000次,通过实习,你可以确保只有一个“john”实际上被分配了内存。
这对于减少程序的内存需求非常有用。但是请注意,缓存是由JVM在永久内存池中维护的,与堆相比,永久内存池的大小通常是有限的,所以如果没有太多重复值,就不应该使用intern。
关于使用intern()的内存约束的更多信息
On one hand, it is true that you can remove String duplicates by internalizing them. The problem is that the internalized strings go to the Permanent Generation, which is an area of the JVM that is reserved for non-user objects, like Classes, Methods and other internal JVM objects. The size of this area is limited, and is usually much smaller than the heap. Calling intern() on a String has the effect of moving it out from the heap into the permanent generation, and you risk running out of PermGen space.
-- 来自:http://www.codeinstructions.com/2009/01/busting-javalangstringintern-myths.html
从JDK 7(我指的是HotSpot)开始,有些东西发生了变化。
In JDK 7, interned strings are no longer allocated in the permanent generation of the Java heap, but are instead allocated in the main part of the Java heap (known as the young and old generations), along with the other objects created by the application. This change will result in more data residing in the main Java heap, and less data in the permanent generation, and thus may require heap sizes to be adjusted. Most applications will see only relatively small differences in heap usage due to this change, but larger applications that load many classes or make heavy use of the String.intern() method will see more significant differences.
——来自Java SE 7的特性和增强
更新:从Java 7开始,被存储的字符串存储在主堆中。http://www.oracle.com/technetwork/java/javase/jdk7-relnotes-418459.html#jdk7changes
其他回答
http://docs.oracle.com/javase/7/docs/api/java/lang/String.html实习生()
基本上,对一系列字符串执行String.intern()将确保具有相同内容的所有字符串共享相同的内存。因此,如果你有一个名字列表,其中“john”出现了1000次,通过实习,你可以确保只有一个“john”实际上被分配了内存。
这对于减少程序的内存需求非常有用。但是请注意,缓存是由JVM在永久内存池中维护的,与堆相比,永久内存池的大小通常是有限的,所以如果没有太多重复值,就不应该使用intern。
关于使用intern()的内存约束的更多信息
On one hand, it is true that you can remove String duplicates by internalizing them. The problem is that the internalized strings go to the Permanent Generation, which is an area of the JVM that is reserved for non-user objects, like Classes, Methods and other internal JVM objects. The size of this area is limited, and is usually much smaller than the heap. Calling intern() on a String has the effect of moving it out from the heap into the permanent generation, and you risk running out of PermGen space.
-- 来自:http://www.codeinstructions.com/2009/01/busting-javalangstringintern-myths.html
从JDK 7(我指的是HotSpot)开始,有些东西发生了变化。
In JDK 7, interned strings are no longer allocated in the permanent generation of the Java heap, but are instead allocated in the main part of the Java heap (known as the young and old generations), along with the other objects created by the application. This change will result in more data residing in the main Java heap, and less data in the permanent generation, and thus may require heap sizes to be adjusted. Most applications will see only relatively small differences in heap usage due to this change, but larger applications that load many classes or make heavy use of the String.intern() method will see more significant differences.
——来自Java SE 7的特性和增强
更新:从Java 7开始,被存储的字符串存储在主堆中。http://www.oracle.com/technetwork/java/javase/jdk7-relnotes-418459.html#jdk7changes
针对Java 8或以上版本进行更新。 在Java 8中,永久生成(Permanent Generation)空间被移除,并被元空间(Meta space)取代。String池内存被移动到JVM的堆中。
与Java 7相比,堆中的String池大小增加了。因此,您可以为内部化的字符串提供更多的空间,但整个应用程序的内存却较少。
还有一件事,你已经知道在Java中比较2个对象的引用时,'=='用于比较对象的引用,'equals'用于比较对象的内容。
让我们检查一下这段代码:
String value1 = "70";
String value2 = "70";
String value3 = new Integer(70).toString();
结果:
Value1 == value2——> true
Value1 == value3——> false
Value1.equals (value3)——> true
Value1 == value3.intern()——> true
这就是为什么你应该使用'equals'来比较2个String对象。这就是intern()的用处所在。
通过使用堆对象引用,如果我们想要对应SCP对象引用,我们应该使用intern()方法。
例子:
class InternDemo
{
public static void main(String[] args)
{
String s1=new String("smith");
String s2=s1.intern();
String s3="smith";
System.out.println(s2==s3);//true
}
}
实习生流程图
面试中有一些“很吸引人”的问题,比如为什么你的成绩和别人一样!如果执行下面的代码段。
String s1 = "testString";
String s2 = "testString";
if(s1 == s2) System.out.println("equals!");
如果你想比较字符串,你应该使用equals()。上面的代码将输出等于,因为testString已经被编译器为你进行了存储。您可以自己使用intern方法实习字符串,如前面的答案....所示
因为字符串是对象,而且Java中的所有对象总是只存储在堆空间中,所以所有字符串都存储在堆空间中。然而,Java将不使用new关键字创建的字符串保存在堆空间的一个特殊区域中,这个区域称为“字符串池”。Java将使用new关键字创建的字符串保存在常规堆空间中。
The purpose of the string pool is to maintain a set of unique strings. Any time you create a new string without using the new keyword, Java checks whether the same string already exists in the string pool. If it does, Java returns a reference to the same String object and if it does not, Java creates a new String object in the string pool and returns its reference. So, for example, if you use the string "hello" twice in your code as shown below, you will get a reference to the same string. We can actually test this theory out by comparing two different reference variables using the == operator as shown in the following code:
String str1 = "hello";
String str2 = "hello";
System.out.println(str1 == str2); //prints true
String str3 = new String("hello");
String str4 = new String("hello");
System.out.println(str1 == str3); //prints false
System.out.println(str3 == str4); //prints false
== operator is simply checks whether two references point to the same object or not and returns true if they do. In the above code, str2 gets the reference to the same String object which was created earlier. However, str3 and str4 get references to two entirely different String objects. That is why str1 == str2 returns true but str1 == str3 and str3 == str4 return false . In fact, when you do new String("hello"); two String objects are created instead of just one if this is the first time the string "hello" is used in the anywhere in program - one in the string pool because of the use of a quoted string, and one in the regular heap space because of the use of new keyword.
字符串池是Java通过避免创建包含相同值的多个String对象来节省程序内存的方法。可以使用string的intern方法从字符串池中获取使用new关键字创建的字符串的字符串。它被称为字符串对象的“实习”。例如,
String str1 = "hello";
String str2 = new String("hello");
String str3 = str2.intern(); //get an interned string obj
System.out.println(str1 == str2); //prints false
System.out.println(str1 == str3); //prints true
OCP Java SE 11程序员,Deshmukh
推荐文章
- Intellij IDEA Java类在保存时不能自动编译
- 何时使用Mockito.verify()?
- 在maven中安装mvn到底做什么
- 不可变与不可修改的集合
- 如何在JSON中使用杰克逊更改字段名
- GSON -日期格式
- 如何从线程捕获异常
- 无法解析主机"<URL here>"没有与主机名关联的地址
- 如何在Java中打印二叉树图?
- String.format()在Java中格式化双重格式
- com.jcraft.jsch.JSchException: UnknownHostKey
- Java中的操作符重载
- 如何加速gwt编译器?
- 如何删除表中特定列的第一个字符?
- 在Hibernate中重新连接分离对象的正确方法是什么?