Two ways to instantiate a String class
A. Direct Assignment
public class Stringlei{public static void Main (string args[]) {string name= "Zhang San"; System.out.println ("Name:" +name);}}
B. Through the keyword new
public class Stringlei{public static void Main (string args[]) {string Name=new string ("Zhang San"); System.out.println ("Name:" +name);}}
content Comparison of string
1. Use "= =" to compare
public class Stringlei{public static void Main (string args[]) { string str1= "Hello"; String Str2=new string ("Hello"); String str3=str2; System.out.println ("Str1==str2?-->" + (STR1==STR2))//false System.out.println ("Str1==str3?-->" + (str1= =STR3)//false System.out.println ("Str2==str3?-->" + (STR2==STR3));//true}}
It can be said that "= =" Determines whether the address space is equal to determine the address, if you want to determine the content, you must use the Equals () method provided in string to complete:
public class Stringlei{public static void Main (string args[]) { string str1= "Hello"; String Str2=new string ("Hello"); String str3=str2; System.out.println ("Str1==str2?-->" + (Str1.equals (str2));//true System.out.println ("Str1==str3?-->" + (Str1.equals (STR3))); /true System.out.println ("Str2==str3?-->" + (Str2.equals (STR3)));//true}}
two ways to instantiate the difference
The constructor method can be completed using the direct assignment and the new call in string. Compare two methods.
A string is an anonymous object of string
public class Stringlei{public static void Main (string args[]) { string str1= "Hello";//actually the point of a heap memory space is given to the stack memory space String str2= "Hello"; String str3= "Hello"; System.out.println ("Str1==str2?-->" + (STR1==STR2))//true System.out.println ("Str1==str3?-->" + (str1= =STR3)//true System.out.println ("Str2==str3?-->" + (STR2==STR3));//true}}
the contents of the string cannot be changed
public class Stringlei{public static void Main (string args[]) { string str= "Hello"; str=str+ "world!"; System.out.println ("str =" +str);}}
This is not a simple change to the contents of STR, as follows:
Java Object-oriented foundation string class