In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article focuses on "how to understand the nature and comparison of Java String classes". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to understand the nature and comparison of Java String classes.
Catalogue
1. What is String?
The construction method of 2.String class
The properties of 3.String class
Comparison between 4.String
1. What is String?
First of all, beginners must know that String is a class, it is a string type, but it does not belong to the basic data class. The so-called string type means that characters (that is, char types) are strung together like kebabs, with columns such as'a 'and' b 'concatenated to form "ab" (note that strings are in double quotes). A 'and' b 'are character types, which are char types, while "ab" is a string type. Ps: the string can be in Chinese
Of course, the above is just a verbal description, but the specific explanation is based on the jdk document.
The construction method of 2.String class
To learn a class, you need to learn its construction method first, and master all its instantiation methods in order to make better use of it. The following is a summary of String class construction methods in jdk documents. (the ps:jdk document is of great help to our learning. It records all the packages and interfaces of java. You can download a Chinese version from the Internet.)
From the jdk document, we can see that there are many ways to construct String classes, but we only need to master the two commonly used ones.
1.public String (char [] value)
Public String (char [] value)
By passing in a char [] to build a String object, the data in char [] is concatenated into a string one by one. The conversion between char and String is often involved in the process of writing code, so this method needs to be memorized.
The following is a code demonstration
Public class myString {public static void main (String [] args) {char [] arr= {'String string = new String (arr); System.out.println (string); / / abcd}}
2.public String (char [] value, int offset, int count)
Public String (char [] value,int offset,int count)
The second construction method is to add two variables on the basis of the first, where offset is the initial offset and count is the length we need to copy, that is, we select a part of the array value to become a String object. This method will be used in some cases, and you need to remember
The following is a code demonstration
Public class myString {public static void main (String [] args) {char [] arr= is the starting subscript for replication, and count is the length of replication}}
In fact, the conversion of an array of byte types to String is the same as char, which is not described here. When we use String, we usually use it directly like common data types to assign values to it. The most important thing to learn String well is to learn its properties.
The properties of 3.String class
It is clearly seen in the jdk documentation that it is modified by finaly in the declaration of the String class, so it is immutable.
But how to understand this immutability?
Public class myString {public static void main (String [] args) {int baked 100; System.out.println (b); / / 100 baked 10; System.out.println (b); / / 10 String a = "aaaa"; System.out.println (a); / / aaaa a = "bbbb"; System.out.println (a); bbbb}
Anyone who sees the printed result of this code will wonder, has the value of a changed from that of b?
But here we need to be clear about a misunderstanding: an is just a reference to a String object, which holds the address of the String object, not the String itself. We access the actual content of the String object by accessing a
We know it through a picture.
After modification:
Figure parsing: we can find that when we will assign "bbbb" to a, the aaaa in the original string constant pool does not change, but a new String object "bbbb" is regenerated in the string constant pool and a points to it. So by immutability, we mean that the content of the String object cannot be changed once it is determined. This requires us to remember all the time.
Through the immutability of the String class, the largest test site for String is created.
Comparison between 4.String
The String class is a very important and common class. We use a lot of scenarios, and it is very common to compare whether the String classes are equal or not. It is necessary to master the ability to compare String.
1.
Public class myString {public static void main (String [] args) {String a1=new String ("AAAA"); String a2=new String ("BBBB"); System.out.println (a1==a2); / / false System.out.println (a2.equals (a2)); / / true}}
Parsing: although A1 and A2 point to the same object content, but the address is different, they do not point to the same object, = = compare the address, while equals compares the content
two。
Public class myString {public static void main (String [] args) {String a1=new String ("AAAA"); String a2rooma1; System.out.println (a1==a2); / / true System.out.println (a2.equals (a2)); / / true}} public static void main (String [] args) {String a1=new String ("AAAA") String a2=new String (A1); System.out.println (a1==a2); / / false System.out.println (a2.equals (a2)); / / true}}
Parsing: String is used as a basic type. The operation of assigning A1 to a2 can be understood as assigning the address of A1 to A2, so both A1 and A2 store the address of "AAAA". The following code gives A1 as a parameter to a2, which re-opens up a piece of memory space for a2 to make its content consistent with A1, but with a different address.
3 、
Public class myString {public static void main (String [] args) {String A1 = "aaaa"; String a2 = "aaaa"; System.out.println (a1==a2); / / true System.out.println (a2.equals (a2)); / / true}}
Parsing: String objects used as basic data types are retained in the string constant pool. Each time this is used, jvm will retrieve whether the constant pool already exists. If it already exists, it will not be created again, but will let the new reference point to the existing String object again.
Summary: we need to use equals for the comparison of String classes, because equals only considers the contents of the string, and = = also needs to consider the address
At this point, I believe you have a deeper understanding of "how to understand the nature and comparison of Java String classes". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.