1. Introduction to
String
In Java, a String
is an object that represents a sequence of characters.
Strings are written using double quotation
marks (" ").
Example:
String s = "Java";
A character
array can also be used to create a String.
Example:
char[] ch = {'j','a','v','a'};
String s = new String(ch);
This is the same as:
String s = "java";
Java StringBuffer Class
The StringBuffer
class is used to create mutable
(modifiable) string objects.
Unlike the String class, StringBuffer objects can be modified after creation.
·
String → Immutable (cannot change)
·
StringBuffer → Mutable (can change)
Features of
StringBuffer
·
Characters can be inserted, replaced, or deleted
·
Automatically grows when more characters are added
·
Stores data in heap memory
· Thread-safe (synchronized)
Important Constructors of StringBuffer
|
Constructor |
Description |
|
|
Creates an empty StringBuffer with initial capacity 16 |
|
|
Creates a StringBuffer with the given string |
|
|
Creates an empty StringBuffer with specified capacity |
Java StringBuilder Class
The StringBuilder
class is also used to create mutable
strings.
It is similar
to StringBuffer, but it is non-synchronized,
meaning it is faster but not thread-safe.
It was introduced in JDK 1.5.
Important Constructors of StringBuilder
|
Constructor |
Description |
|
|
Creates an empty StringBuilder with capacity 16 |
|
|
Creates a StringBuilder with the specified string |
|
|
Creates an empty StringBuilder with given capacity |
StringBuffer Methods
1. append() Method
The append()
method adds a string at the end of the existing string.
Example:
class StringBufferExample {
public static void main(String args[]) {
StringBuffer sb = new StringBuffer("Hello ");
sb.append("Java"); // original string changes
System.out.println(sb);
}
}
Output
Hello Java
insert() Method
The insert()
method inserts a string at a specified position.
Example:
class StringBufferExample2 {
public static void main(String args[]) {
StringBuffer sb = new StringBuffer("Hello ");
sb.insert(1, "Java");
System.out.println(sb);
}
}
Output
HJavaello
3. replace() Method
The replace()
method replaces characters between the specified index positions.
Example:
class StringBufferExample3 {
public static void main(String args[]) {
StringBuffer sb = new StringBuffer("Hello");
sb.replace(1, 3, "Java");
System.out.println(sb);
}
}
Output
HJavalo
4. delete() Method
The delete()
method removes characters between specified indexes.
Example:
class StringBufferExample4 {
public static void main(String args[]) {
StringBuffer sb = new StringBuffer("Hello");
sb.delete(1, 3);
System.out.println(sb);
}
}
Output
Hlo
5. reverse() Method
The reverse()
method reverses the string.
Example:
class StringBufferExample5 {
public static void main(String args[]) {
StringBuffer sb = new StringBuffer("Hello");
sb.reverse();
System.out.println(sb);
}
}
Output
olleH
Difference Between String and
StringBuffer
|
No |
String |
StringBuffer |
|
1 |
Immutable (cannot be changed) |
Mutable (can be changed) |
|
2 |
Creates new object when modified |
Modifies existing object |
|
3 |
Slower for concatenation |
Faster for concatenation |
|
4 |
Overrides |
Does not override |
Java String length() Method
The length()
method returns the number of characters in a string.
Syntax
public int length()
Example:
class CalcLength {
public static void main(String args[]) {
String name = "Santosh";
int length = name.length();
System.out.println("The length of the String \"" + name + "\" is: " + length);
}
}
Output
The length of the String "Santosh" is: 7
7. Java String concat() Method
The concat()
method joins two or more strings together.
Syntax
string1.concat(string2)
Example:
public class ConcatExample {
public static void main(String[] args) {
String str1 = "Hello ";
String str2 = "Ramesh ";
String str3 = "Sharma";
String str4 = str1.concat(str2);
System.out.println(str4);
String str5 = str1.concat(str2).concat(str3);
System.out.println(str5);
}
}
Output
Hello Ramesh
Hello Ramesh Sharma
Comments
Post a Comment