String Operations

String

The String class represents character strings. All string literals in Java programs, such as,”abc” are implemented as instances of this class.
Strings are constant; their values cannot be changed after they are created. String buffers support mutable strings. Because String objects are immutable they can be shared.

Example

is equivalent to:

Example

    • The class String includes methods for examining individual characters of the sequence, for comparing strings, for searching strings, for extracting substrings, and for creating a copy of a string with all characters translated to uppercase or to lowercase.
    • Case mapping is based on the Unicode Standard version specified by the Character class.
    • The Java language provides special support for the string concatenation operator ( + ), and for conversion of other objects to strings.
    • String concatenation is implemented through the StringBuilder(or StringBuffer) class and its append method.
    • String conversions are implemented through the method toString, defined by Object and inherited by all classes in Java.

Note: String is a class in java and is immutable

StringBuffer

  • A thread-safe, mutable sequence of characters. A string buffer is like a String but can be modified. At any point in time, it contains some particular sequence of characters, but the length and content of the sequence can be changed through certain method calls
  • String buffers are safe for use by multiple threads. The methods are synchronized where necessary so that all the operations on any particular instance behave as if they occur in some serial order that is consistent with the order of the method calls made by each of the individual threads involved
  • The principal operations on a StringBuffer are the append and insert methods, which are overloaded so as to accept data of any type. Each effectively converts a given datum to a string and then appends or inserts the characters of that string to the string buffer
  • The append method always adds these characters at the end of the buffer; the insert method adds the characters at a specified point

Let us look at this example indication different usage of string and StringBuffer:

If you were to use StringBuffer to concatenate your code need to look like this:

StringBuilder

  • The java.lang.StringBuilder class should be used when you have to make a lot of modifications to strings of characters. Objects of type StringBuilder can be modified over and over again without leaving behind a great effluence of discarded String objects. StringBuilder
  • Objects are the ideal way to handle a block of data, pass it on, and then reuse the same memory to handle the next block of data
  • This class provides an API compatible with StringBuffer, but with no guarantee of synchronization.
  • This class is designed for use as a drop-in replacement for StringBuffer in places where the string buffer was being used by a single thread (as is generally the case). Where possible, it is recommended that this class be used in preference to StringBuffer as it will be faster under most implementations.
  • The StringBuilder class was added in Java 5. It has exactly the same API as the StringBuffer class, except StringBuilder is not thread-safe i.e its methods are not synchronized.

The principal operations on a StringBuilder are the append and insert methods, which are overloaded so as to accept data of any type. Each effectively converts a given datum to a string and then appends or inserts the characters of that string to the string builder. The append method always adds these characters at the end of the builder; the insert method adds the characters at a specified point.
Let’s see an example explaining the above concept:

It is because no new assignments are made. Now, when we use StringBuilder as:

So here we are not creating any extra objects, each one needs only a single StringBuffer object to execute.

Leave a Reply

Contact Us
close slider
Contact Us

How can we help you with?