mod6
mod6
String Handling
In Java, a String is a sequence of characters. It is a widely used data type that represents
text. Strings are immutable, meaning that once a String object is created, its value cannot be
L
changed. Any operation that appears to modify a string will actually create a new string.
A
What is String in Java?
Generally, String is a sequence of characters. But in Java, string is an object that represents a
sequence of characters. The java.lang.String class is used to create a string object.
D
Characteristics of Strings
N
● Immutable: Once a String object is created, it cannot be changed. Any modification
creates a new String.
● String Pool: Java optimizes memory usage through a concept known as the String Pool.
A
When a string is created, it is stored in a special memory area, and if another string with
the same value is created, the reference to the original string is returned instead of
creating a new object.
M
● Character Encoding: Strings in Java are stored in Unicode, allowing for representation
of characters from multiple languages.
In Java, you can create a String in several ways, either by using literals or by calling
constructors. Here’s a guide on each method:
LA
When you create a string using double quotes, Java automatically creates a String
object and stores it in a special memory area called the String Pool.
IP
This method is generally recommended for creating strings as it’s more efficient due to
the use of the String Pool.
B
● Using the new Keyword
You can also create a String by calling the String constructor with the new keyword.
This approach creates a new String object on the heap, even if an identical string
already exists in the String Pool.
L
A
This is less efficient than using string literals, as it bypasses the String Pool and creates
a new object.
D
● Using a Character Array
You can create a String from a character array by passing the array to the String
N
constructor.
● Using String.valueOf()
The String.valueOf() method is used to convert different types of values (like int,
float, char, etc.) into a String.
IP
B
● Using Concatenation
You can also create a String by concatenating literals, variables, or other strings. Java
internally uses StringBuilder to optimize this operation.
Common String Methods
L
Java provides numerous methods for string manipulation. Here are some of the most commonly
A
used methods:
D
● Length:
N
● Character Extraction:
A
M
● Substring:
B
● Concatenation:
LA
String Comparison
IP
● Equality:
B
● Lexicographic Comparison:
L
● Index Search:
A
D
● Contains:
N
● Replace:
A
M
Case Conversion
● To Lowercase:
B
LA
● To Uppercase:
IP
● Trim:
B
● Format:
L
● toString(): Returns the string itself.
A
D
● valueOf(): Returns the string representation of different data types.
N
StringBuffer Class
A
The StringBuffer class is used to create mutable strings, meaning the contents can be
modified after creation. It is generally preferred when a lot of modifications are required.
M
Constructors
StringBuffer Operations
● Append
○ StringBuffer append(String str): Appends the specified string to this
character sequence.
IP
● Insert
B
L
● Reverse
○ StringBuffer reverse(): Reverses the characters in the string buffer.
A
D
● Capacity and Length
○ int capacity(): Returns the current capacity of the string buffer.
N
○ int length(): Returns the number of characters currently stored.
Conversion to String
●
A
String toString(): Converts the StringBuffer to a String object.
M
Key Differences Between String and StringBuffer
B