Monday, 30 January 2012

String handling in Java


Strings:
•A string is a sequence/group of characters.
•Strings in java are implemented as objects of the class “String” unlike other languages which implement strings using character arrays.
•Once a string(object) is created, we cannot change the characters in that string. Rather, we can create a new string object.
•All kind of string operations like concatenation, copying, case inversion and substring and many others can be performed on string objects.
•To create mutable(changeable) strings we create objects for the “StringBuffer” or “StringBuilder” classes.
•All the classes: String, StringBuffer and StringBuilder are available in “java.lang” package. As this package is by default imported into the program, there is no need to import it explicitly for our string classes to work in the program.
•All these three string classes: String, StringBuffer and StringBulider are declared as “final”. So, they cannot be inherited.

StringBuffer:
•StringBuffer is another class with which we can create and manipulate strings in our programs.
•The difference between String and StringBuffer is, the strings created using String class are immutable(non-changeable) where as strings created using StringBuffer class are mutable(changeable).

StringBuilder:
•StringBuilder is another class with which we can create mutable or changeable strings.
•Difference between StringBuffer and StringBuilder is, StringBuffer is synchronized(thread safe) where as StringBuilder is not synchronized(not thread safe).
•Another difference is StringBuilder gives high performance, where as StringBuffer gives low performance.
•For multithreaded applications it is better to use StringBuffer and for other applications it is better to use StringBuilder.
•All the StringBuilder’s constructors and methods are very similar to StringBuffer.
•For the constructors and methods available in the StringBuilder class please copy and paste the following link in the browser:
http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/StringBuilder.html

No comments:

Post a Comment