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 to use it properly.
Given heavy use
of Java String in almost any kind of project, it become even more important to
know subtle detail about String. In this java tutorial
we will see some important points about Java String, which is worth
remembering.
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.
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++, String in Java
doesn't terminate with null character. Instead String are Object in Java and
backed by character array. You can get the character array used to represent
String in Java by calling toCharArray() method of java.lang.String class of
JDK.
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 String. Since String
is final there is no way anyone can extend String or override any of String
functionality.
3) Strings are maintained in String
Pool
As I Said earlier String is special
class in Java and all String literal e.g. "abc" (anything which is inside double quotes are
String 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 String object using String 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 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 contains same contents.
5) Use indexOf() and lastIndexOf() or matches(String regex) method to search inside String
String class in Java provides
convenient method to see if a character or sub-string or a pattern
exists in current String object. You can use indexOf() which will return
position of character or String, if that exist in current String object or -1
if character doesn't exists in String. l
6) Use SubString to get part of String in Java
Java String 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 you number of characters in String, including white spaces like tab,
space. One point which is worth remembering here is that substring is also
backed up by character array, which is used by original String.
7) "+" is overloaded for
String concatenation
Java doesn't support Operator
overloading but String is special and +
operator can be used to concatenate two Strings. It can even used to convert int,
char, long or double to convert into String by simply concatenating with empty
string "". 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 String. Since both represent mutable object they can be used
to reduce string garbage created because of temporary String.
8) Use trim() to remove white spaces
from String
String in Java provides trim()
method to remove white space from both end 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. replaceAll method even support 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 individual part in a
String array.
10) Don't store sensitive data in String
String pose 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 risk of being seen by
anyone who has access to Java memory, like reading from memory dump. Instead char[]
should be used to store password or sensitive information.
No comments:
Post a Comment