10 Things Every Java Programmer Should Know About String At Infocampus

Given heavy use of Java String in almost any kind of project, it become even more important to know subtle detail about String. Though I have shared lot of String related article already here in Javarevisited, this is an effort to bring some of String feature together. In this tutorial we will see some important points about Java , which is worth remembering. You can also refer my earlier post 10 advanced Java questions to know more about String.

Though I tried to cover lot of things, there are definitely few things, which I might have missed; please let me know if you have any question or doubt on java.lang.String functionality and I will try to address them here.

1) Strings are not null terminated in Java.

Unlike C and C++, in Java doesn't terminate with null character. Instead are Object in Java and backed by character array. You can get the character array used to represent in Java 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 content of String. If you modify it by using toLowerCase(), toUpperCase() or any other method, It always result in new . Since is final there is no way anyone can extend or override any of functionality. Now if you are puzzled why String is immutable or final in Java. checkout the link.

3) Strings are maintained in String Pool

As I Said earlier is special class in Java and all literal e.g. "abc" (anything which is inside double quotes are literal in Java) are maintained in a separate String pool, special memory location inside Java memory, more precisely inside PermGen Space. Any time you create a new object using literal, JVM first checks String pool and if an object with similar content available, than it returns that and doesn't create a new object. JVM doesn't perform String pool check if you create object using 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 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 contains same contents.

5) Use indexOf() and lastIndexOf() or matches(String regex) method to search inside String

class in Java provides convenient method to see if a character or sub- or a pattern exists in current object. You can use indexOf() which will return position of character or , if that exist in current object or -1 if character doesn't exists in . lastIndexOf is similar but it searches from end. .match(regex) is even more powerful, which allows you to search for a regular expression pattern inside .

6) Use Sub to get part of in Java

Java provides another useful method called sub(), which can be used to get parts of . basically you specify start and end index and substring() method returns character from that range. Index starts from 0 and goes 1. By the way .length() returns you number of characters in String, including white spaces like tab, space. One point which is worth remembering here is that sub is also backed up by character array, which is used by original . This can be dangerous if original object is very large and is very small, because even a small fraction can hold reference of complete array and prevents it from being garbage collected even if there is no other reference for that particular .

7) "+" is overloaded for String concatenation

Java doesn't support Operator overloading but is special and + operator can be used to concatenate two . It can even used to convert int, char, long or double to convert into by simply concatenating with empty ". internally + is implemented using StringBuffer prior to Java 5 and StringBuilder from Java 5 onwards. This also brings point of using StringBuffer or StringBuilder for manipulating .

8) Use trim() to remove white spaces from

Java provides trim() method to remove white space from both end . If trim() removes white spaces it returns a new otherwise it returns same . Along with trim() also provides replace() and replaceAll() method for replacing characters from . replaceAll method even support regular expression.

9) Use split() for splitting using Regular expression

Java is feature rich. it has methods like split(regex) which can take any in form of regular expression and split the based on that. particularly useful if you dealing with comma separated file (CSV) and wanted to have individual part in a array.

10) Don't store sensitive data

pose security threat if used for storing sensitive data like passwords, SSN or any other sensitive information. Since is immutable in Java there is no way you can erase contents of and since they are kept in pool (in case of String literal) they stay longer on Java heap ,which exposes risk of being seen by anyone who has access to Java memory, like reading from memory dump.