Home > Java > javaTutorial > Comprehensive analysis of String object data type in Java

Comprehensive analysis of String object data type in Java

高洛峰
Release: 2017-01-19 14:35:27
Original
1532 people have browsed it

1. First of all, String does not belong to the 8 basic data types. String is an object.

Because the default value of the object is null, the default value of String is also null; but it is a special object and has some characteristics that other objects do not have.

2. New String() and new String("") both declare a new empty string, which is an empty string and not null;

3. String str="kvill";

String str=new String ("kvill"); The difference:
Here, we do not talk about the heap, nor the stack, but simply introduce the simple concept of the constant pool.
The constant pool (constant pool) refers to the pool that is determined at compile time and saved in the compiled pool. Some data in the class file. It includes constants in classes, methods, interfaces, etc., as well as string constants.

Look at Example 1:

String s0="kvill"; 
String s1="kvill"; 
String s2="kv" + "ill"; 
System.out.println( s0==s1 ); 
System.out.println( s0==s2 );
Copy after login

The result is:
true
true
First of all, we need to know that the result is that Java will ensure that there is only one copy of a string constant.
Because the "kvill" in s0 and s1 in the example are both string constants, they are determined at compile time, so s0==s1 is true; and "kv" and "ill" are also characters String constants, when a string is concatenated by multiple string constants, it must itself be a string constant, so s2 is also parsed into a string constant at compile time, so s2 is also "kvill" in the constant pool " a reference.
So we get s0==s1==s2;
The string created with new String() is not a constant and cannot be determined at compile time, so the string created with new String() does not contain constants In the pool, they have their own address space.
Look at Example 2:

String s0="kvill"; 
String s1=new String("kvill"); 
String s2="kv" + new String("ill"); 
System.out.println( s0==s1 ); 
System.out.println( s0==s2 ); 
System.out.println( s1==s2 );
Copy after login

The result is:
false
false
false
In Example 2, s0 is still the application of "kvill" in the constant pool, because s1 cannot be used in It is determined at compile time, so it is a reference to the new object "kvill" created at runtime. Because s2 has the second half of newString("ill"), it cannot be determined at compile time, so it is also an application of the newly created object "kvill"; Once you understand this, you will understand why this result is obtained.

4. String.intern():

Let me add one more point: exists in. The constant pool in the class file is loaded by the JVM during runtime and can be expanded. The intern() method of String is a method to expand the constant pool; when a String instance str calls the intern() method, Java checks whether there is a string constant with the same Unicode in the constant pool. If so, it returns its reference. If If not, add a Unicode string equal to str in the constant pool and return its reference; it will be clear by looking at Example 3
Example 3:

String s0= "kvill"; 
String s1=new String("kvill"); 
String s2=new String("kvill"); 
System.out.println( s0==s1 ); 
System.out.println( "**********" ); 
s1.intern(); 
s2=s2.intern(); //把常量池中"kvill"的引用赋给s2 
System.out.println( s0==s1); 
System.out.println( s0==s1.intern() ); 
System.out.println( s0==s2 );
Copy after login

The result is:
false
**********
false //Although s1.intern() is executed, its return value is not assigned to s1
true //Indicates that s1.intern() returns a constant Reference to "kvill" in the pool
true
Finally, I will dispel a misunderstanding:
Someone said, "Use the String.intern() method to save a String class to a global String table , if the Unicode string with the same value is already in this table, then this method returns the address of the string already in the table. If there is no string with the same value in the table, register your own address in the table." If I understand the global String table he mentioned as a constant pool, his last sentence, "If there is no string with the same value in the table, register your own address in the table" is wrong:
Look at Example 4:

String s1=new String("kvill"); 
String s2=s1.intern(); 
System.out.println( s1==s1.intern() ); 
System.out.println( s1+" "+s2 ); 
System.out.println( s2==s1.intern() );
Copy after login

结果为: 
false 
kvill kvill 
true 
在这个类中我们没有声名一个"kvill"常量,所以常量池中一开始是没有"kvill"的,当我们调用s1.intern()后就在常量池中新添加了一个"kvill"常量,原来的不在常量池中的"kvill"仍然存在,也就不是"将自己的地址注册到常量池中"了。 
s1==s1.intern()为false说明原来的"kvill"仍然存在; 
s2现在为常量池中"kvill"的地址,所以有s2==s1.intern()为true. 

5. 关于equals()和==: 

这个对于String简单来说就是比较两字符串的Unicode序列是否相当,如果相等返回true;而==是比较两字符串的地址是否相同,也就是是否是同一个字符串的引用。 

6. 关于String是不可变的 

这一说又要说很多,大家只要知道String的实例一旦生成就不会再改变了,比如说:String str="kv"+"ill"+" "+"ans"; 
就是有4个字符串常量,首先"kv"和"ill"生成了"kvill"存在内存中,然后"kvill"又和" " 生成 "kvill "存在内存中,最后又和生成了"kvill ans";并把这个字符串的地址赋给了str,就是因为String的"不可变"产生了很多临时变量,这也就是为什么建议用StringBuffer的原因了,因为StringBuffer是可改变的。

更多Java中的String对象数据类型全面解析相关文章请关注PHP中文网!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template