String in Java is very special class and most frequently used class as well. There are lot many things to learn about String in Java than any other class, and having a good knowledge of different String functionalities makes you use it properly.
Given heavy use of it in almost any kind of project, it becomes even more important to know subtle detail about String.In this tutorial, we will see some important points about Java String, which is worth remembering.
Though I tried to cover a lot of things, there are definitely few things, which I might have missed;
- 1 1) Strings are not null terminated in Java
- 1.1 2) Strings are immutable and final in Java
- 1.2 3) Strings are maintained in String Pool
- 1.3 4) Use Equals methods for comparing String in Java
- 1.4 5) Use indexOf() and lastIndexOf() or matches(String regex) method to search inside String
- 1.5 6) Use SubString to get part of String in Java
- 1.6 8) Use trim() to remove white spaces from String
- 1.7 9) Use split() for splitting String using Regular expression
- 1.8 10) Don’t store sensitive data in String
- 1.9 Share this:
- 1.10 Like this:
- 1.11 Related
1) Strings are not null terminated in Java
Unlike C and C++, String in Java doesn’t terminate with the null character. Instead, String is Object in Java and backed by character array. You can get the character array used to represent String by calling toCharArray() method of java.lang.String class of JDK.
2) Strings are immutable and final in Java
Strings are immutable in Java it means once created you cannot modify the content of String. If you modify it by using toLowerCase(), toUpperCase() or any other method, It always results in new String. Since String is final there is no way anyone can extend String or override any of String functionality. Now if you are puzzled why String is immutable or final in it.
3) Strings are maintained in String Pool
As I Said earlier String is the special class in Java and all String literal e.g. “ABC” (anything which is inside double quotes is String literal) are maintained in a separate String pool, special memory location inside Java memory, more precisely inside PermGen Space. Any time you create a new String object using String literal, JVM first checks String pool and if an object with similar content available, then it returns that and doesn’t create a new object. JVM doesn’t perform String pool check if you create an object using the new operator.
4) Use Equals methods for comparing String in Java
String class overrides equals method and provides a content equality, which is based on characters, case, and order. So if you want to compare two String object, to check whether they are same or not, always use equals() method instead of equality operator. Like in earlier example if we use equals method to compare objects, they will be equal to each other because they all contain same contents.
5) Use indexOf() and lastIndexOf() or matches(String regex) method to search inside String
String class in Java provides the convenient method to see if a character or sub-string or a pattern exists in the current String object. You can use indexOf() which will return the position of character or String if that exists in current String object or -1 if the character doesn’t exist in String. lastIndexOf is similar but it searches from the end. String.match(String regex) is even more powerful, which allows you to search for a regular expression pattern inside String.
6) Use SubString to get part of String in Java
It provides another useful method called substring(), which can be used to get parts of String. basically, you specify start and end index and substring() method returns character from that range. Index starts from 0 and goes till String.length()-1. By the way String.length() returns your number of characters in String, including white spaces like tab, space.
7) “+” is overloaded for String concatenation
It doesn’t support Operator overloading but String is special and the + operator can be used to concatenate two Strings. It can even use to convert int, char, long or double to convert into String by simply concatenating with the empty string “. internally + is implemented using StringBuffer prior to Java 5 and StringBuilder from Java 5 onwards. This also brings the point of using StringBuffer or StringBuilder for manipulating String.
8) Use trim() to remove white spaces from String
String in Java provides trim() method to remove whitespace from both ends of String. If trim() removes white spaces it returns a new String otherwise it returns same String. Along with trim() String also provides replace() and replaceAll() method for replacing characters from String. the replaceAll method even supports regular expression.
9) Use split() for splitting String using Regular expression
String in Java is feature rich. it has methods like split(regex) which can take any String in form of regular expression and split the String based on that. particularly useful if you dealing with comma separated file (CSV) and wanted to have the individual part in a String array.
10) Don’t store sensitive data in String
String pose the security threat if used for storing sensitive data like passwords, SSN or any other sensitive information. Since String is immutable in Java there is no way you can erase contents of String and since they are kept in String pool (in case of String literal) they stay longer on Java heap , which exposes the risk of being seen by anyone who has access to Java memory, like reading from memory dump.