The examples in this article summarize the usage of strings in Java. Share it with everyone for your reference. The specific analysis is as follows:
The essence of a string is an array of char type, but in Java, all strings declared with double quotes "" are objects of the String class. This also reflects the completely object-oriented language characteristics of Java.
String class
1. The String class object represents a constant string. It is of immutable length. In other words, once an instance of the String class is created, the string represented by this instance cannot be changed. Similar to
str = str + "Hello";
This operation essentially creates a new String object by combining the two String objects str and "Hello", and then assigns the reference of the new String object to str. Extensive use of this operation can cause performance flaws. If you need to change the content of the string frequently, you should use the StringBuffer class or the StringBuilder class. We can use a small program to see how big the performance difference is between the String and StringBuffer classes.
class StringTest { public static void main(String[] args) { /** * Perform 10,000 append operations using String objects and StringBuffer objects respectively * Test execution time * 2013.3.18 */ String constStr = ""; long lTime = System .currentTimeMillis(); for(int i = 0; i < 10000; ++i) constStr += i; System.out.println("Const String:"+(System.currentTimeMillis()-lTime)); StringBuilder strBuf = new StringBuilder(""); lTime = System.currentTimeMillis(); for(int i = 0 ; i < 10000 ; ++i) strBuf.append(String.valueOf(i)); System.out.println("Buffered String: "+(System.currentTimeMillis()-lTime)); } }Execution result:
It can be seen that StringBuilder takes seconds, but the String class takes 300ms, which shows the huge performance difference.
Both StringBuilder and StringBuffered represent a variable-length (mutable) string object. The difference between them is that the StringBuffered class does some safety processing in terms of thread synchronization, while StringBuilder does the opposite. So if you are only programming in a single line, StringBuilder is slightly more efficient than StringBuffered (actually the difference is not big)
2. "Hello World" is a String object. We can use "Hello World" directly as an object, such as:
if("Hello".equals("Hello")) System.out.println("Yes");The output result is Yes.
3. When comparing string objects, you must use the equals() method instead of simply using == to judge. Because == compares whether the instances referenced by two reference names are the same, the equals() method compares whether the strings in the two objects are equal.
A common question is, how many String objects does the following code snippet generate in total?
String str1 = new String("Hello");String str2 = new String("Hello");Probably most people will answer, 2. But in fact, a total of 3 String objects are generated here. In addition to str1 and str2, don't forget that "Hello" is also a String object.
4. String pool (Pool)
Java maintains a String pool when executing. When a string declared by double quotes appears, the JVM will first check whether the same String object (with the same characters) exists in the memory. If it exists, it will return a reference to the object that already exists in the memory. If it does not exist, it will create it. A new String object. This saves memory. As shown in the following piece of code, str1 and str2 actually point to the same String object.
String str1 = "Hello";String str2 = "Hello";
5. Receive command line parameters
When we declare the main method, we declare a formal parameter of type String[]. This array of String objects stores the command line parameters passed in by the user when executing this program. Note that unlike the C language, command line parameters in Java start from the first variable after the program name. In other words, String[] data does not include the application name. For example:
class strCmd { public static void main(String[] args) { if(args.length > 0) { for(String str : args) System.out.println(str); } } }The output when executing java strCmd Hello World! is:
6. Separation of strings
Similar to strtok in C language, the String class also has a split method that can separate a string in a specified format. The split method returns an array of String objects, representing each separated string. like:
class strSplit { public static void main(String[] args) { String str = "Hello/tWorld/tI/tLove/tYou!"; System.out.println("Original String : " + str); String[] strArr = str.split("/t"); for(String s : strArr) System.out.println(s); } }Among them, the parameters of the split() method can be regular expressions. You can use the static method matches() of the Matcher class in the java.util package to determine whether a string matches a regular expression.
The String class has many other powerful functions, and we should learn to read the API documentation. Here the importance of English is reflected...
I hope this article will be helpful to everyone’s Java programming.