Top 10 questions of Java Strings

The following are top 10 frequently asked questions about Java Strings.

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

In brief, "==" tests if references are equal and equals() tests if values are equal. Unless you want to check if two strings are the same object, you should always use equals().

It would be better if you know the concept of string interning.

2. Why is char[] preferred over String for security sensitive information?

Strings are immutable, which means once they are created, they will stay unchanged until Garbage Collector kicks in. With an array, you can explicitly change its elements. In this way, security sensitive information(e.g. password) will not be present anywhere in the system.

3. Can we use string for switch statement?

Yes to version 7. From JDK 7, we can use string as switch condition. Before version 6, we can not use string as switch condition.

// java 7 only!
switch (str.toLowerCase()) {
      case "a":
           value = 1;
      case "b":
           value = 2;

4. How to convert string to int?

int n = Integer.parseInt("10");

Simple, but so frequently used and sometimes ignored.

5. How to split a string with white space characters?

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

6. What substring() method really does?

In JDK 6, the substring() method gives a window to an array of chars which represents the existing String, but do not create a new one. To create a new string represented by a new char array, you can do add an empty string like the following:

str.substring(m, n) + ""

This will create a new char array that represents the new string. The above approach sometimes can make your code faster, because Garbage Collector can collect the unused large string and keep only the sub string.

In Oracle JDK 7, substring() creates a new char array, not uses the existing one. Check out the diagram for showing substring() difference between JDK 6 and JDK 7.

7. String vs StringBuilder vs StringBuffer

String vs StringBuilder: StringBuilder is mutable, which means you can modify it after its creation.
StringBuilder vs StringBuffer: StringBuffer is synchronized, which means it is thread-safe but slower than StringBuilder.

8. How to repeat a string?

In Python, we can just multiply a number to repeat a string. In Java, we can use the repeat() method of StringUtils from Apache Commons Lang package.

String str = "abcd";
String repeated = StringUtils.repeat(str,3);

9. How to convert string to date?

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

10. How to count # of occurrences of a character in a string?

Use StringUtils from apache commons lang.

int n = StringUtils.countMatches("11112222", "1");

One more
Do you know How to detect if a string contains only uppercase letter?

Category >> Basics >> Top 10  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
String foo = "bar";
  • abanoub wagih

    # of occurrence

    public static int countOccurrencesOfSubstring(String str, String sub) {
    return str.split(Pattern.quote(sub), -1).length - 1;


    public static boolean isOnlyNumeric(String str) {
    Pattern pattern = Pattern.compile("^[-]?\d+(\.\d+)?$");
    return pattern.matcher(str).matches();

  • ricardo rio

    Thanks for your information. Regards, TNPSC Exam Notifications Chennai

  • Zhao

    蛮不错的 文

  • very nice and must visit java top faqs of java string programs here

  • mojiayi

    Oh…I must do more research on apache commons series tool package

  • Jacky

    So nice article,thanks.

  • 939347507


  • Thomas Mueller

    About 2. Security: the old char[] contents _could_ still be kept in memory, when using a copying collector that copies the old char array to another memory location before it is wiped, or swapped to disk if there is not enough physical memory. I know those are edge cases, but it’s possible. But using char[] is better than using a String of course.

  • ryanlr


  • jeanroldao

    Hi, I think that item 8 is wrong… Shouldn’t it be this? (Correct me if I’m wrong)
    String str = “abc”;
    String repeated = StringUtils.repeat(str, 3);

  • Java Developer

    nice article on string