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 nice to know the concept of a string pool .
-
Why
char[]
is string (String
) preferred over sensitive information?Strings are immutable . This means that once a string is created, it will not change until the garbage collector mechanism is run. In the case of an array, it is quite obvious that we can change its elements. Thus, confidential information (for example, 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. Earlier in the 6th version, this was not possible.
// java 7 only! switch (str.toLowerCase()) { case "a": value = 1; break; case "b": value = 2; break; }
-
How to convert string to integer value?
int n = Integer.parseInt("10");
The same is true for other Java primitive types.
-
How to split a string into lines with a space delimiter?
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) + ""
As a result, we get 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 showing the difference between the substring() methods in JDK 6 and JDK 7 . -
String vs StringBuilder vs StringBuffer
String
vsStringBuilder
:StringBuilder
mutable, which means we can change its content after the object has been created.StringBuilder
versusStringBuffer
:StringBuffer
synchronized, which means that it functions correctly when used from multiple threads at the same time, but is slower thanStringBuilder
. -
How to repeat a string multiple times?
In Python, we can simply multiply a string by a number
n
and it will then repeat n times. In Java, we can use the methodrepeat()
from theStringUtils
Apache Commons Lang library class.String str = "abcd"; String repeated = StringUtils.repeat(str,3); //abcdabcdabcd
-
How to convert string to 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);