It can be like this:
Set< string & gtset = new HashSet & lt string & gt ();
String? s 1? = "Hello";
String s2 = " world
set . add(s 1);
set . add(S2);
It can be like this:
Set< string & gtset = new HashSet & lt string & gt ();
String? s 1? = "Hello";
String s2 = " world
set . add(s 1);
set . add(S2);
This adds two elements.
Extended data:
A summary of string class learning in Java
A string is an immutable object.
The java.lang.String class is decorated with final and cannot be inherited. All words in Java programs, that is, strings enclosed in double quotation marks, such as "abc", are implemented as instances of string classes. String is a constant, and its object cannot be changed once it is constructed.
Example:
Public classes are immutable {
Public Static String Capitalization (String) (
Returns s.touppercase ();
}
Public static void main(String[] args) {
string str 1 = " Hello World ";
system . out . println(str 1); ? //Hello world
string str 2 = up case(str 1);
system . out . println(str 2); ? //Hello world
system . out . println(str 1); ? //Hello world
}
}
Second, the string constant pool
Constant pool is defined at compile time and saved after compilation. Class file. In order to improve performance, Java creates static strings in a constant pool, and tries to use the same object and reuse static strings. For repeated string literals, the JVM will first look in the constant pool and return the object if it exists in the constant pool.
Example:
Public class test 1 {
Public static void main(String[] args){
string str 1 = " Hello ";
//Instead of creating a new String object, use the existing "Hello" in the constant pool.
String str2 = " Hello
system . out . println(str 1 = = str 2); //true
//Using the new keyword will create a new string object.
String str3 = new string ("hello");
system . out . println(str 1 = = str 3); //False?
}
}