seven invariance of strings
A string object cannot be modified after it is created, and is immutable, so-called modification is actually creating a new object that points to a different memory space. Such as
As shown:
Operation Result:
In combination with the above code, there is a need to know about strings:
1) through string s1= "Love Class"; Declares a string object that S1 a reference to a string object, storing reference relationships in memory such as
As shown:
Then through s1= "Welcome to:" +S1; Changed the string S1, which essentially creates a new string object, and the variable S1 points to the newly created character
The string object, as shown in:
2) Once a string is created in memory, the string will not be changed. If you need a string that can be changed, we can use the
StringBuffer class or StringBuilder class.
3) Each time a new string is created, even if the contents of the two strings are the same, "=" is compared with "false", as
Fruit you should use the Equals () method only if you want to compare whether the content is the same. both S3 and S4 are new objects created in heap memory through new, with their respective addresses,
and = = comparison is the address, the thought false.
Instance:
public class Test {public static void Main (string[] args) { String S1 = "IMOOC"; String s2 = "IMOOC"; Define the string S3, save "I Love" and S1 the contents of the concatenation of string s3= "I Love" +S1; Compare strings S1 and S2 //IMOOC as constant strings, which are optimized by the compiler when they occur more than once, creating only one object System.out.println ("S1 and S2 memory addresses are the same?"). "+ (S1 = = s2)); Compare strings S1 and S3 System.out.println ("S1 and S3 memory addresses the same?" "+ (S1==S3)); String s4 = "I Love" + S1; Comparing strings S4 and S3 //S1 are variables, S4 know the specific values at run time, so S3 and S4 are different objects System.out.println ("S3 and S4 memory addresses are the same?"). "+ (S4 = = S3));} }
Compile run Result:
Explain:
S1 is the existence of a constant pool of strings, so its address is given by a constant pool. The same S2 is also the address of the constant pool, the address is the string often
The address of "IMOOC", so the address of S1 and S2 is the same. However S3 is a constant plus a variable , so there is a heap in memory, this address
is a heap of memory new to S3. Similarly, S4 's address is also a heap of memory for him. So the memory addresses of S3 and S4 are not the same.
Eight general methods of string class
The string class provides many methods for working with strings, such as getting the length of a string, intercepting a string, and converting a string to uppercase
or lowercase, string segmentation, and so on.
Common methods of the string class one:
combine the code to familiarize yourself with the use of the method:
public class test{public static void Main (string[] args) { String S1 = "Sun Java"; String s2 = "Sun Java"; System.out.println (S1.charat (1)),//u System.out.println (S2.length ());//8 System.out.println (s1.indexof ("Java")); /4 System.out.println (S1.indexof ("Java")),//-1 System.out.println (s1.equals (S2));//false System.out.println (s1.equalsignorecase (S2));//true String s = "I am a programmer, I am learning java"; String str = s.replace (' Me ', ' you '); System.out.println (str);} }
Operation Result:
String Class common method two:
Instance code:
public class test{public static void Main (string[] args) { String s = ' Welcome to Java world! '; String S1 = "Sun Java"; System.out.println (S.startswith ("Welcome"))//true System.out.println (S.endswith ("World"));//false String SL = S.tolowercase (); String SU = S.touppercase (); System.out.println (SL); System.out.println (SU); String subs = s.substring (one); SYSTEM.OUT.PRINTLN (subs); String sp = S1.trim (); SYSTEM.OUT.PRINTLN (sp);} }
Operation Result:
Precautions:
1) The index of the character in the string S2 starts at 0 and ranges to Str.length ()-1.
2) when using IndexOf for character or string lookups, if the match returns a position index, or 1 if there is no matching result.
3) when using substring (beginindex, endIndex) for string interception, include the character of the beginindex position, excluding the endIndex position of the
Character.
String Class common method three:
Static overloaded methods
public static String valueOf (...) You can convert a base type to a string
For example:
public static string ValueOf (double D) public static string valueOf (int i)
Method public string[] Split (string regex) splits a string by the specified delimiter, returning the segmented array of strings.
Instance code:
public class test{public static void Main (string[] args) { int j = 1234567; String Snumber = string.valueof (j); System.out.println ("J is" +snumber.length () + "number of digits"); String s = "mary,f,1976"; string[] split = S.split (","); for (int i=0;i<split.length;i++) { System.out.println (split[i]);}}}
Operation Result:
nine similarities and differences between "= =" and Equals () in the String class
1) = =: Determine whether the first address of the two string in memory is the same, that is, whether it is the same string object.
2) equals (): Compares whether the contents stored in two string objects are consistent.
These two usages we have used in many places in the front, here is no longer a detailed example of their similarities and differences, want to learn more about the foot
article of the House:the difference between equals and equals sign (= =) in Java.
Javase Getting Started learning 28:java Common class string class (bottom)