In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to understand the string String on the basis of Java". Interested friends may wish to have 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 string String on the basis of Java.
String constant is immutable
The underlying source code uses final to modify char [] value to store the value of the string. String constants exist in the constant pool, and once declared, they cannot be changed, and strings with the same content are not stored in the constant pool, that is, S1 and S2 are equal.
String S1 = "aaa"
S1 = "bbb"
System.out.println (S1)
String S1 = "123"
String S2 = "123"
System.out.println (s1==s2)
The value of the output S1 is bbb, isn't it changed? Note that S1 here is a reference object, so S1 exists in the stack space, that is, S1 and aaa do not exist in the same memory space, but a rope is connected to point aaa to S1. Now this rope points from bbb to S1, but aaa actually remains in the constant pool, so string constants are immutable.
In addition, String also implements the Serializable interface, which indicates that the string can be serialized, and the Comparable interface that indicates that the string can be relatively small.
The difference between null and ""
String S1 = null
String S2 = ""
Null represents an empty object, not a string, and can be assigned to any object. The string represents just a reference, and there is no memory space allocation.
"" means that the reference has already pointed to a piece of memory space, and it is a practical thing that can be operated to represent a string of length 0.
Array to string (construction of String)
String (): construct an empty string
String (byte [] arr): turns a byte array into a string
String (byte [] arr, int offset, int lengh): change part of a byte array into a string
String (char [] arr): change the char byte array into a string
String (char [] arr, int offset, int length): change part of the char byte array into a string
String (String original): string constants build strings
Byte [] = = > String
All conversion: convert all byte data which is a good http://www.zykdtj.com/ in Zhengzhou fetus expelling Hospital
Byte [] b = {97, 981, 99100}
String str = new String (b)
System.out.println (str); / / abcd
Partial conversion: intercepts the conversion, exceeds the index, and reports a StringIndexOutOfBoundsException exception. Usually, the array index interval in Java is left open and right closed.
Byte [] b = {97, 981, 99100}
String str = new String (bmeme1mai 3)
System.out.println (str); / / bc
Char [] to String and so on, some arrays to String are more or less the same as above, which are based on the String method.
= whether the comparison string with equals () is equal
The address and content of the comparison are equal only if they are equal.
The content of equals () is equal
Understand the above two sentences:
String S1 = "123"; ①
String S2 = "123"; ②
String S3 = new String ("123"); ③
System.out.println (s1==s2); / / correct
System.out.println (s1==s3); / / error
five
As long as you understand the memory distribution, judgment is not a problem. For ① and ②, it has been explained above that they are all in stack memory. For ③, S3 means that objects are stored in the Java heap, and S1 Magi S2 is stored in the Java stack, so s1==s3 is wrong! It's not the same for equals (). All three are equal.
Exercise 1:
String S1 = new String ("hello")
String S2 = new String ("hello")
System.out.println (s1==s2)
System.out.println (s1.equals (S2))
String S3 = new String ("hello")
String S4 = "hello"
System.out.println (s3==s4)
System.out.println (s3.equals (S4)
String S5 = "hello"
String S6 = "hello"
System.out.println (s5==s6)
System.out.println (s5.equals (S6)
Concatenation of strings
The splicing of constant and constant is still in the constant pool.
Constant pool cannot have the same constant
When splicing, all variables are stored in the heap as long as there are variables.
Call the intern () method to return the constants in the constant pool
String S1 = "hello"
String S2 = "world"
String S3 = "helloworld"
The join of System.out.println (s3percent = (s1+s2)); / / F variable is not equal in the heap
System.out.println (s3 equal = (s1+s2) .intern ()); / / T gets the same value
System.out.println (s3.equals (s1+s2)); / / T gets equal content
System.out.println (s3percent = "hello" + "world"); / / constant and constant connections are still in the constant pool
System.out.println (s3.equals ("hello" + "world")); / / T content is equivalent
Common methods of string manipulation
Judgment of string:
The equals method compares whether the contents of two strings are equal
EqualsIgnorecase ignores case to compare whether two objects are equal
Whether contains contains strings
Whether startsWith () begins with the specified string
Whether endsWIth () ends with the specified string
Whether isEmpty () is empty
String S1 = "abcde"
String S2 = "AbCde"
String S3 = "abcde"
/ / equals
System.out.println (s1.equals (S2)); / / t
System.out.println (s1.equals (S3)) / / f
/ / equalsIgnorecase
System.out.println (s1.equalsIgnoreCase (S2)); / / t
System.out.println (s1.equalsIgnoreCase (S3)); / / t
/ / whether to include the specified string
System.out.println (s1.contains ("bd")) / / f
/ / whether to start with the specified string
System.out.println (s1.startsWith ("ab")); / / t
System.out.println (s1.startsWith ("cde", 2)); / / whether the string is intercepted at the beginning of index 2
/ / whether to end with a string
System.out.println (s1.endsWith (S3)); / / t
/ / whether it is empty
System.out.println (s1.isEmpty ()); / / false
eighteen
Acquisition of string
Length (): length of the string
CharAt (inx index): returns the index of a character in a string
IndexOf (int ch): gets the position where the specified character first appears in the string, and you can write the corresponding ASCALL code value.
IndexOf (int ch, int fromIndex): the position where the character appears starting from the specified index
IndexOf (String str): gets the position of the specified string in the original string
IndexOf (String str, int fromIndex): gets the location where the string first appears, starting with the specified index
LastIndexOf (int ch): gets the index value of the last occurrence of the specified character
LastIndexOf (String str,int fromIndex) gets the index value of the last occurrence of the specified string
SubString (int start): intercepts a string from a specified location
SubString (int start, int end) intercepts a string from a specified location to a specified location
At this point, I believe you have a deeper understanding of "how to understand the string String on the basis of Java". 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.