Top 10 most asked questions about strings in Java are mentioned here
-
How to compare strings? What to use: "
==
" orequals()
?In short, "
==
" compares references to strings, butequals()
compares the values contained in those strings. If you don't want to check that two strings are the same object, then you should useequals()
.It would also be a good idea to know the concept of a string pool .
-
Why
char[]
are strings (String
) preferred for protecting sensitive information?Strings are immutable . This means that once a string is created, it will not be modified until the garbage collector is run. In the case of an array, it is quite obvious that we can change its elements. This way, sensitive information (such as a password) will not be stored in clear text anywhere in the program.
-
Can we pass strings as a parameter when using a switch statement ?
Yes, starting from version 7 of the JDK. Previously, in version 6, this was not possible.
// java 7 only! switch (str.toLowerCase()) { case "a": value = 1; break; case "b": value = 2; break; }
-
How to convert a string to an integer value?
int n = Integer.parseInt("10");
The same goes for other primitive types in Java.
-
How to split a string into space delimited lines?
The easiest way is to use regular expressions. "
\s
" acts as a substitute for whitespace characters such as "\t
", "\r
", "\n
".String[] strArray = aString.split("\\s+");
-
< What does the method actually do
substring()
?In JDK 6, the method
substring()
showed a portion of the characters from an existing string, but did not create a separate instance of the string. To create a new string, represented as an array of characters, you can add some empty string, for example, like this:str.substring(m, n) + ""
The result is an array of characters, which is our new string. This approach can sometimes reduce code execution time because the garbage collector can destroy unused large strings and store only substrings.
In Oracle JDK 7, the method
substring()
creates a new character array without using an existing one. You can also take a look at the diagram representing the difference between substring() methods in JDK 6 and JDK 7 . -
String vs StringBuilder vs StringBuffer
String
vsStringBuilder
:StringBuilder
mutable, this means that we can change its content after the object is created.StringBuilder
vsStringBuffer
:StringBuffer
synchronized, meaning that it functions correctly when used from multiple threads at the same time, but is slower thanStringBuilder
. -
How to repeat a line several times?
In Python, we can simply multiply a string by a number
n
and then it will repeat n times. In Java, we can use a methodrepeat()
from theStringUtils
Apache Commons Lang library class.String str = "abcd"; String repeated = StringUtils.repeat(str,3); //abcdabcdabcd
-
How to convert a string to a date?
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
-
How to count the number of occurrences of a certain character in a string?
Use a class
StringUtils
from the Apache Commons Lang library.int n = StringUtils.countMatches("11112222", "1"); System.out.println(n);
GO TO FULL VERSION