0% found this document useful (0 votes)
4 views

mod6

The document provides an overview of string handling in Java, highlighting that strings are immutable objects represented by the java.lang.String class. It explains various ways to create strings, common string methods, and introduces the StringBuffer class for mutable strings. Key differences between String and StringBuffer, including mutability and performance, are also discussed.

Uploaded by

mandalipsita2002
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

mod6

The document provides an overview of string handling in Java, highlighting that strings are immutable objects represented by the java.lang.String class. It explains various ways to create strings, common string methods, and introduces the StringBuffer class for mutable strings. Key differences between String and StringBuffer, including mutability and performance, are also discussed.

Uploaded by

mandalipsita2002
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Web Technology - Module VI

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.

Ways to Create String


B

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

● Using String Literals

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 a Byte Array


A
M
A String can also be created from a byte array, with each byte in the array
representing an ASCII character.
B
LA

● 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:

Basic String Operations

D
● Length:

N
● Character Extraction:

A
M
● Substring:
B

● Concatenation:
LA

String Comparison
IP

● Equality:
B
● Lexicographic Comparison:

Searching and Replacing

L
● Index Search:

A
D
● Contains:

N
● Replace:

A
M
Case Conversion

● To Lowercase:
B
LA

● To Uppercase:
IP

Trimming and Formatting

● Trim:
B
● Format:

Conversion to Other Data Types

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(): Creates an empty string buffer with a default capacity.


● StringBuffer(String str): Creates a string buffer initialized to the specified string.
B

● StringBuffer(int capacity): Creates a string buffer with the specified initial


capacity.
LA

StringBuffer Operations

● Append
○ StringBuffer append(String str): Appends the specified string to this
character sequence.
IP

● Insert
B

○ StringBuffer insert(int offset, String str): Inserts the specified


string at the specified position.
● Delete
○ StringBuffer delete(int start, int end): Removes the characters in
a substring from the string buffer.

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

Feature String StringBuffer

Mutability Immutable Mutable


LA

Less efficient for More efficient for


Performance
modifications modifications

Thread Safety Not synchronized Synchronized

Usage For fixed strings For dynamic strings


IP
B

You might also like