There are two ways to instantiate strings in java, namely:
1. Direct assignment
String str = "Hello World";
2. Constructor instantiation
String str = new String("Hello World");
(Video tutorial recommendation: java video)
Specific example:
//直接赋值 public class StringDemo{ public static void main(String[] args) { String str1 = "Hello qty"; String str2 = "Hello qty"; } } //构造方法实例化 public class StringDemo{ public static void main(String[] args) { String str1 = new String("Hello qty"); } }
Recommended tutorial: java entry program
The above is the detailed content of How to define string in java. For more information, please follow other related articles on the PHP Chinese website!