1. Run the following sample code Stringpool.java to see its output. How do I interpret this output? What can you sum up from it?
In Java, a string constant with the same content ("Hello") holds only one copy to conserve memory, so s0,s1,s2 actually refers to the same object. When the compiler compiles a s2 sentence, it strips out the "+" number and connects the two strings directly to a string ("Hello"). This optimization work is done automatically by the Java compiler. When you create a string object directly using the New keyword, the value is the same (all "Hello"), but it is still two separate objects.
2. Why does the above output result? What can you sum up from it?
Assigning a string variable means: Two variables (S1,S2) now refer to the same string object "a"!
The contents of the string object are read-only, and the value of the S1 variable is modified with "+", in effect, a new string object is obtained with the content "AB", which is independent of the object "A" referenced by the original S1, so s1==s2 returns false;
The "AB" string in the code is a constant that refers to a string that is not related to the "AB" object referenced by S1. but if "s1=" AB ", then Judge" s1== "AB", the result is true.
The String.Equals () method can compare the contents of a two string.
3. Please review the implementation Code of the String.Equals () method and learn how to implement it.
PublicclassStringequals { PublicStaticvoidMain (string[] args) {String s1=NewString ("Hello"); String s2=NewString ("Hello");System.out.println (s1==S2); System.out.println (s1.equals (S2));String s3= "Hello"; String s4= "Hello";System.out.println (s3==S4); System.out.println (S3.equals (S4)); } }
The Equals () or Equalsignorecase () method compares the contents of the two strings, using = = to compare whether the two-string variable refers to the same string object;
CompareTo: Using dictionary method to compare, return 0 table two strings equal, less than return negative value, greater than return positive;
Regionmatches: Compares whether a part of a two-character string is equal.
4.Length (): Get string length
1 The Length property in Java is said for an array, for example, you declare an array, and you want to know the length of the array is used to this property.
2 the Length () method in Java is for string strings, and the length () method is used if you want to see how long the string is.
The size () method in 3.java is said for a generic collection, and if you want to see how many elements the generic has, call this method to view it!
charAt (): gets the character at the specified position
Str.charat (): Method returns the Char value at the specified index. The index range is from 0 to length ()-1. For an array index, the first char value of a sequence is at index 0, index 1, and so on.
getChars (): gets the substring copied from the specified position into the character array.
Str.getchars (int srcbegin, int srcend, char[] DST, int dstbegin)
The first character to be copied at index Srcbegin, the last character copied is at the index srcEnd1 that is, the total number of characters to be copied is Srcend Srcbegin.
The character is copied to the sub-array of daylight saving time starting at index Dstbegin and ending at index:dstbegin + (srcend-srcbegin)-1
Replace (): substring substitution
Str.replace (char Oldchar,char Newchar) returns a new string that is generated by replacing all OldChar that appear in this string with Newchar.
touppercase (), toLowerCase (): Uppercase and lowercase conversions
str.touppercase/() Str.tolowercase (), will all the characters in this string be uppercase.
trim (): Remove the Kinsoku space:
Str.trim (), which returns a copy of the string, but strips out the white space at the beginning and end of the string, and returns a copy of the string if the string ends without padding.
ToCharArray (): converts a String object to a character array
Str.tochararray () returns a newly assigned character array whose length is the length of the string, and the content is initialized to a sequence of characters that contains this string representation.
Java the fourth chapter after class finishing