SoFunction
Updated on 2025-03-08

Ten Most Common Java String Problems (Translation)

Translated from:Top 10 questions of Java Strings

1. How to compare strings? Use "==" or equals()?

Simply put, "==" tests whether the references of the two strings are the same, and equals() tests whether the values ​​of the two strings are the same. Unless you want to check if two strings are the same object, it is better to use equals().
If you knowString resideThe mechanism will be better.

2. Why is security-sensitive information char[] better than String?

The string is unchanged, which means that once the string is created, they will remain there until they are cleaned up by the garbage collector. And for an array, you can explicitly modify its elements. In this way, security-sensitive information (such as passwords) will not appear anywhere else in the system.

3. Can we use String in switch statements?

The answer to Java7 is yes. Starting with JDK7, we can use String as a condition for the switch statement. Before JDK6, we could not use String as a condition for switch statements.

// java 7 only!
switch (()) {
   case "a":
      value = 1;
      break;
   case "b":
      value = 2;
      break;
}

4. How to convert a string to an integer?

int n = ("10");

 

It's very simple, used too often and sometimes it's ignored.

5. How to decompose a string with blank characters?

We can simply use regular expressions to decompose. "\s" represents a blank character, for example " ","\t", "\r", "\n".

String[] strArray = ("\\s+");

 

What does the () method really do?

In JDK6, the substring() method provides a window representing a character array of existing strings, but does not create a new string. To create a new string represented by a new character array, add an empty string like below:

(m, n) + ""

 

This creates a brand new character array representing the new string. The above method sometimes makes the code faster because the garbage collector will collect large unused strings and only keep a substring.
In Oracle JDK 7, substring() creates a new character array without using the existing array.The substring() Method in JDK 6 and JDK 7 The chart in this illustrates the difference between substring() of JDK 6 and JDK 7.

vs StringBuilder vs StringBuffer

String vs StringBuilder: StringBuilder is mutable, which means that after creation, people can change its value.
StringBuilder vs StringBuffer: StringBuffer is synchronous, which means it is thread-safe, but slower than StringBuilder.

8. How to repeat a string?

In Python, we can repeat a string by multiplying by a number. In Java, we can repeat strings through the repeat() method of the StringUtils class in the Apache Commons Lang package.

String str = "abcd";
String repeated = (str,3);
//abcdabcdabcd

 

9. How to convert a string to a date?

String str = "Sep 17, 2013";
Date date = new SimpleDateFormat("MMMM d, yy", ).parse(str);
(date);
//Tue Sep 17 00:00:00 EDT 2013

 

10. How to count the number of times a character appears in a string?

Use the StringUtils class in the Apache Commons Lang package.

int n = ("11112222", "1");
(n);

 

Additional questions
How to detect that a string contains only capital letters

Translated from:Top 10 questions of Java Strings