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

Comprehensive analysis of String object data type in Java

Jan 19, 2017 pm 02:35 PM

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中文网!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Apr 19, 2025 pm 04:51 PM

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

How to simplify field mapping issues in system docking using MapStruct? How to simplify field mapping issues in system docking using MapStruct? Apr 19, 2025 pm 06:21 PM

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

How to elegantly obtain entity class variable names to build database query conditions? How to elegantly obtain entity class variable names to build database query conditions? Apr 19, 2025 pm 11:42 PM

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

How do I convert names to numbers to implement sorting and maintain consistency in groups? How do I convert names to numbers to implement sorting and maintain consistency in groups? Apr 19, 2025 pm 11:30 PM

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

How to safely convert Java objects to arrays? How to safely convert Java objects to arrays? Apr 19, 2025 pm 11:33 PM

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products? E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products? Apr 19, 2025 pm 11:27 PM

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to elegantly get entity class variable name building query conditions when using TKMyBatis for database query? How to elegantly get entity class variable name building query conditions when using TKMyBatis for database query? Apr 19, 2025 pm 09:51 PM

When using TKMyBatis for database queries, how to gracefully get entity class variable names to build query conditions is a common problem. This article will pin...

See all articles