10 interview questions about String

Below are the most easily asked questions about String in an interview.


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


Simply put, "==" tests whether the references of two objects are the same, and equals() compares whether the values ​​of the two strings are equal. Unless you want to check whether two strings are the same object, you should use equals() to compare strings.


If you know the concept of interning, so much the better.


2. Why is char[] better than String for information with high security and confidentiality?


Because String is immutable, that is, once it is created, it cannot be changed until the garbage collector collects it. The elements in the character array can be changed (Translator's Note: This means that you can change it after using it without retaining the original data). Therefore, if a character array is used, high-security information (such as passwords) will not exist in the system and be seen by others.


3. Can we use switch conditional statements for strings?


For JDK 7, the answer is yes. Starting from JDK 7, we can use switch conditional statements for strings; in JDK 6 or earlier versions, we cannot use switch conditional statements.


// Java 7 or later

switch (str.toLowerCase()) {

      case "a":

           value = 1;

           break;

      case "b":

           value = 2;

           break;

}


4. How to convert a string to int?


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


Very simple and often used, but often overlooked.


5. How to separate the string with blank characters?


We can use regular expressions to split characters. "\S" stands for blank characters "", "\t", "\r", "\n".


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


6. What exactly does the substring() method do?


In JDK 6, the method of substring() is to use a character array to represent an existing string, and then provide a "window" to this character array, but it does not actually create a new character array. To create a new string object represented by a new string array, you need to add an empty string, as shown below:


str.substring(m, n) + ""


This will create a new character array to represent the new string. This method will make your code faster because the garbage collector will collect unused long strings and only save the substrings to be used.


In Oracle JDK 7, substring() creates a new character array instead of using an existing character array. Click to see the difference between substring() in JDK 6 and JDK 7 .


http://www.importnew.com/7418.html


7. String vs StringBuilder vs StringBuffer


String vs StringBuilder: StringBuilder is mutable, which means you can still change its value after it is created.

StringBuilder vs StringBuffer: StringBuffer is synchronized, it is thread-safe, but slower than StringBuilder.


8. How to repeat a string


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


String str = "abcd";

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

// abcdabcdabcd


9. How to convert a string to time


String str = "Sep 17, 2013";

Date date = new SimpleDateFormat("MMMM d, yy", Locale.ENGLISH).parse(str);

System.out.println(date);

//Tue Sep 17 00:00:00 EDT 2013


10. 如何计算一个字符串某个字符的出现次数?


请使用apache commons lang包中的StringUtils:


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

System.out.println(n);


Guess you like

Origin blog.51cto.com/15082395/2591194