Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use String in Java

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail how to use String in Java. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Preface

String can be said to be the most frequently used and special class in Java because it is also a literal constant, while literal constants include basic types, String types, and null types.

one。 The use of String

1. The immutability of String

/ * * The {@ code String} class represents character strings. All * string literals in Java programs, such as {@ code "abc"}, are * implemented as instances of this class. *

* Strings are constant; their values cannot be changed after they * are created. String buffers support mutable strings. * Because String objects are immutable they can be shared. For example: *... * / public final class String {private final char value [];}

Once the String object is created in the heap, it cannot be modified. Because the String object is placed in the char array, the array is modified by the final keyword and is immutable.

two。 Define a string

/ * define a string * / String str1 = "helloworld"; String str2 = "helloworld"; / / it is also possible, but basically does not write String str3 = new String ("helloworld"); System.out.println (str1 = = str2); System.out.println (str1 = = str3); / / run result true, false

How to understand the above three sentences of code? Here we need to introduce a concept, string constant pool.

String constant pool is a special independent memory space, which is placed in Java Heap {before Jdk7.0, string constant pool is stored in PermGen, when Jdk7.0 is moved to Java Heap (it is still independent in the heap), when Jdk8.0 is removed, PermGen is removed and Metaspace is used to replace}. The Java memory model is not the focus of this chapter.

The string literals referenced by str1 and str2 are in the string constant pool, while the objects referenced by str3 are in Java Heap.

What, it's not easy to understand? For instance

After a day's work, when it's time to get off work, I'm ready to read the golden bottle for a while. Forget it. "Romance of the three Kingdoms", open the novel website, and read it online; after half an hour, the girl friend went home, and watching the Romance of the three Kingdoms was also what she wanted to do. I saw the URL sent to her. OK, she also began to read it. Half an hour later, my father came back. He was also a fan of the three Kingdoms, but he didn't like to read it online, so he bought a copy in the bookstore.

The novel website mentioned above is a constant pool of strings, which contains a lot of literals, such as Romance of the three Kingdoms, Journey to the West, A Dream of Red Mansions, etc., and each string literal remains unique in the constant pool. no matter who goes to the website to read the Romance of the three Kingdoms is the same URL and the same content.

My girlfriends and I are str1 and str2. We both watch the Romance of the three Kingdoms of the same website, not only with the same content, but also with the same referenced address (the unique "helloworld" is retained in the string constant pool), so str1 = = str2 runs as true.

And my father is str3, which is different from me and my girlfriends, although the content is also "Romance of the three Kingdoms", but through physical books, the citation address is different, and a book does not support multiple people to read at the same time (string objects are in java heap, and each time new will create a new object), so str1 = = str3 runs for false.

The literal amount of a string always refers to the same instance of the String class because it is qualified by the String.intern () method, which can also be called to put the String objects in the heap into the string constant pool, which improves memory efficiency and allows consumers to share a unique instance.

System.out.println (str1 = = str3.intern ()); / / the running result is true

So what is the implementation logic of this method? let's take a look at the source code.

/ * Returns a canonical representation for the string object. *

* A pool of strings, initially empty, is maintained privately by the * class {@ code String}. *

* When the intern method is invoked, if the pool already contains a * string equal to this {@ code String} object as determined by * the {@ link # equals (Object)} method, then the string from the pool is * returned. Otherwise, this {@ code String} object is added to the * pool and a reference to this {@ code String} object is returned. *

* It follows that for any two strings {@ code s} and {@ code t}, * {@ code s.intern () = = t.intern ()} is {@ code true} * if and only if {@ code s.equals (t)} is {@ code true}. *

* All literal strings and string-valued constant expressions are * interned. String literals are defined in section 3.10.5 of the * The Java ™Language Specification. * @ return a string that has the same contents as this string, but is * guaranteed to be from a pool of unique strings. * / public native String intern ()

We find that this is a native method. Take a look at the comments and find that the general flow of the str3.intern () method is:

When intern () is executed, it first determines whether there is the same string literal in the string constant pool (through the equals method), and if it does not, the string object is added to the string constant pool and a reference to the string constant pool is returned.

The returned reference needs to be assigned, otherwise it will still point to the address in the heap, that is:

String str4 = new String ("helloChina"); System.out.println (str4.intern () = = str4); / / falsestr4 = str4.intern (); String str5 = "helloChina"; String str6 = "helloZhonghua" System.out.println (str4 = = str5); / / true

Let's take a look at the memory structure.

3. Assign a value to the defined string again

Str6 = "helloHuaxia"

We have already said that String is modified by the final keyword and is immutable, so how is it reflected in memory at this time?

4. String's treatment of "+"

String str7 = "good good" + "study"; String str8 = "good good study"; system.out.println (str7 = = str8)

After compiling the tool, you get the

String str7 = "good good study"; String str8 = "good good study"

So we can find that the compiler does variable merging during compilation instead of creating three objects "good good", "study" and "good good study" in the constant pool. Str7 = = str8 run result true.

But if so,

String str9 = "good good"; String str10 = str9 + "study"; system.out.println (str8 = = str10); / / false

At this time, the running result is false, and the result obtained by String variable + character constant will be in the heap, not in the constant pool. Of course, you can put it into the constant pool through the intern () method, and not only "+", but also call substring (), toUpperCase (), trim (), etc. all return the address of String in the heap.

5. Methods commonly used in String

/ / str1 = = "hello,world"; / / get the length str1.length () / / 12 ello / intercept the string between position 2 and 5 (including position 2, excluding position 5, starting from 0) str1.substring (2 llo); / / "llo" / / determine whether it contains the string "ello" str1.contains ("ello"); / / true, implement / / obtain the start position str1.indexOf ("ello") of ello in str1 through indexOf / / 1ax / convert a string to string data str1.split (","); / / ["hello", "world"] / / remove the spaces on both sides of the string str1.trim () / / this is the end of the article "hello,world" on "how to use String in Java". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report