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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

How does Java's classloading mechanism work, including different classloaders and their delegation models? How does Java's classloading mechanism work, including different classloaders and their delegation models? Mar 17, 2025 pm 05:35 PM

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa

How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache? How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache? Mar 17, 2025 pm 05:44 PM

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading? How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading? Mar 17, 2025 pm 05:43 PM

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution? How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution? Mar 17, 2025 pm 05:46 PM

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management? How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management? Mar 17, 2025 pm 05:45 PM

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

See all articles