One, strings (string)
1. In Java, strings are processed as objects of type string. The String class is located in the Java.lang package. By default, the package is automatically imported into all programs.
2, how to initialize the string
Defines a string
S1 = "Java";
Creates an empty string
s2 = new string ();
Creates an empty string and assigns a
string s3 = new String ("Com.lemon");
3, the invariance of the string: string objects can not be modified after creation, is immutable, the so-called modification is actually created a new object, pointing to different memory space.
4. 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 StringBuffer or StringBuilder (as we'll find in later chapters).
5, each time new a string is to produce a new object, even if the contents of two strings are the same, use "= =" compared to "false", if only to compare the same content, you should use the "Equals ()" method
Example:
String S01 = "Java";
String S02 = "Java";
String S03 = "I love" + S01;
String s4 = "I Love" + S01;
Comparing strings S01 and S02
//IMOOC is a constant string, which is optimized by the compiler when it occurs multiple times, creating only one object, True
System.out.println ("S01 and S02 memory addresses are the same." "+ (S01 = = S02));
Compare string S01 and S03 false
System.out.println ("S01 and S03 memory addresses the same. "+ (S01 = = S03) );
S01 is a variable, S04 knows the specific value at run time, so S03 and S04 are different objects false
System.out.println ("S03 and S04 memory addresses the same." "+ (S4 = = S03));
Analogy
String object1 = new String ("OC");
String object2 = new String ("OC");
String object3 = "OC";
= = comparison is the memory address of the referencing object false
System.out.println (Object1 = = object2);
= = compares the memory address of two referenced objects false
System.out.println (Object1 = = object3); equals comparison value True
System.out.println (Object1.equals (object2));
Demo