Two ways to instantiate a String class
Example. Assigning values directly to String class objects
public class StringDemo { public static void main(String args[]){ String str = "www.Baidu.com"; System.out.println(str); } }
www.Baidu.com
The code above does not use the keyword new, but the String class itself defines a construction method with the following syntax:
public String(String str)
Example. Instantiate using a construction method
public class StringDemo { public static void main(String args[]){ String str = new String("www.Baidu.com"); System.out.println(str); } }
www.Baidu.com
String comparison 1
Example. Determine if two integers of type int are equal
public class StringDemo { public static void main(String args[]){ int x = 10; int y = 10; System.out.println(x==y); } }
true
This program directly defines two int s. In Java,'=='can be applied to all data types, including basic and reference data types.
Example. Compare using'=='on String objects
public class StringDemo { public static void main(String args[]){ String stra = "hello"; String strb = new String("hello"); String strc = strb; System.out.println(stra==strb); System.out.println(stra==strc); System.out.println(strb==strc); } }
false false true
In the above analysis, stra and strb contain exactly the same content, but they are in different memory address spaces. When using'=='comparison, only numeric values are compared, so'==' cannot achieve an accurate string comparison.
Example. Implement string content comparison
public class StringDemo { public static void main(String args[]){ String stra = "hello"; String strb = new String("hello"); String strc = strb; System.out.println(stra.equals(strb)); System.out.println(stra.equals(strc)); System.out.println(strb.equals(strc)); } }
true true true
In this example, the equals() method is used to compare the contents, and the specific syntax is:
A.equals(B)---------- Determine if A and B content are equal
Note:
- "==": is a java-provided relational operator whose main function is to make a numeric equality judgment, if used on a String object to represent a comparison of memory address values;
- "equals()": A method provided by String that is specifically responsible for comparing the contents of strings
The difference between two instantiations
1. Direct Assignment Instantiates String Class Objects
Direct assignment is the setting of a name to an anonymous object of a string. The syntax is as follows:
String variable = string constant (anonymous object)
String str = "hello";
2. Construction method instantiates String class objects
String str = new String("hello");
Since each string is an anonymous object of the String class, a space is first opened in the heap memory to hold the string "hello", followed by another heap memory space using the new keyword. So the heap memory opened up with the new keyword is really used, and the initial space will become garbage.
Example. Do not automatically save object pool operations
public class StringDemo { public static void main(String args[]){ String stra = "hello"; String strb = new String("hello"); System.out.println(stra==strb); } }
false
If you want to open up new memory data for object pooling, you can use a manual pooling operation defined by the String class with the following syntax:
public String intern();
Example. Manual Pooling
public class StringDemo { public static void main(String args[]){ String stra = "hello"; String strb = new String("hello").intern(); System.out.println(stra==strb); } }
true
Summary: In all development, instantiation of String objects is always done by direct assignment