This article introduces the 10 most common questions about String in Java:
1. For string comparison, should you use "==" or equals()?
Simply put, "==" determines whether two references refer to the same memory address (the same physical object).
And equals determines whether the values of two strings are equal.
Unless you want to determine whether two string references are the same object, you should always use the equals() method.
You will understand this problem better if you understand string interning
2. For sensitive information, why is it better to use char[] than String?
String is an immutable object, which means that once created, the entire object cannot be changed. Even if a novice thinks that the String reference has changed, it is actually just a (pointer) reference pointing to another (new) object.
Programmers can explicitly modify the character array, so sensitive information (such as passwords) is not easily exposed elsewhere (as long as you set char[] to 0 after use).
3. Use String as case condition in switch statement?
Starting from JDK7, this is possible. By the way, Java 6 and previous versions do not support this.
Copy the code code as follows:
// Only valid in java 7 and above!
switch (str.toLowerCase()) {
case "a":
value = 1;
break;
case "b":
value = 2;
break;
}
4. Convert String to number
For very large numbers please use Long, the code is as follows
Copy the code code as follows:
int age = Integer.parseInt("10");
long id = Long.parseLong("190"); // If the value may be large.
5. How to split string by whitespace characters
The string received by String's split() method will be parsed as a regular expression.
"/s" represents white space characters, such as space " ", tab "/t", line feed "/n", carriage return "/r".
When the compiler parses the source code, it will also perform literal transcoding, so "//s" is needed.
Copy the code code as follows:
String[] strArray = aString.split("//s+");
6. How is the substring() method processed internally?
In JDK6, the substring() method still shares the original char[] array and constructs a "new" String through offset and length.
If you want substring() to obtain a newly created object, use the following method:
Copy the code code as follows:
String sub = str.substring(start, end) + "";
Of course, in Java 7, substring() creates a new char[] array instead of sharing it.
To learn more, please refer to: substring() method and its differences in JDK6 and JDK7
7. String vs StringBuilder vs StringBuffer
StringBuilder is mutable, so the internal values can be modified after creation.
StringBuffer is synchronized and therefore thread-safe, but relatively less efficient.
8. How to splice the same string repeatedly?
Option 1: Use the StringUtils utility class of the Apache Commons Lang library.
Copy the code code as follows:
String str = "abcd";
String repeated = StringUtils.repeat(str,3);//abcdabcdabcd
Option 2:
Use StringBuilder constructor. More flexible.
Copy the code code as follows:
String src = "name";
int len = src.length();
int repeat = 5;
StringBuilder builder = new StringBuilder(len * repeat);
for(int i=0; i<repeat; i++){
builder.append(src);
}
String dst = builder.toString();
9. How to convert String to Date?
Copy the code code as follows:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String str = "2013-11-07";
Date date = format.parse(str);
System.out.println(format.format(date));//2013-11-07
10. How to count the number of occurrences of a certain character?
Also using the Apache Commons Lang library StringUtils class:
Copy the code code as follows:
int n = StringUtils.countMatches("11112222", "1");
System.out.println(n);