0% found this document useful (0 votes)
6 views23 pages

1.8 String

The document provides an overview of various string handling classes in Java, including String, StringBuilder, StringBuffer, and StringTokenizer. It explains how to create string objects, their immutability, and methods for string operations such as concatenation, comparison, and substring extraction. Additionally, it highlights the differences between mutable and immutable string classes and their respective methods.

Uploaded by

fibeti7925
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views23 pages

1.8 String

The document provides an overview of various string handling classes in Java, including String, StringBuilder, StringBuffer, and StringTokenizer. It explains how to create string objects, their immutability, and methods for string operations such as concatenation, comparison, and substring extraction. Additionally, it highlights the differences between mutable and immutable string classes and their respective methods.

Uploaded by

fibeti7925
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

SringTokenizer Class

StringBuilder Class
StringBuffer Class
Substring
String Operations
String Class
String
Rajesh kumar
CORE JAVA
 In Java, string is basically an object that represents sequence of char values.
 An array of characters works same as Java string.
 Generally, String is a sequence of characters. But in Java, string is an object that

CORE JAVA
SringTokenizer Class

represents a sequence of characters. The java.lang.String class is used to create a


StringBuilder Class
StringBuffer Class

String Operations string object.

String Class
Substring

How to create a string object?


 There are two ways to create String object:String
 By string literal
By Dineshkumar
 Example:
String s="welcome";
 By new keyword
Example:
String s=new String("Welcome");
//creates two objects and one reference variable
 A String is an unavoidable type of variable while writing any application program.
 String references are used to store various attributes like username, password,

CORE JAVA
etc.
SringTokenizer Class
StringBuilder Class
StringBuffer Class

String Operations
 In Java, String objects are immutable. Immutable simply means unmodifiable or
unchangeable.

String Class
Substring

 Once String object is created its data or state can't be changed but a new String
object is created.
String
 String class in java is final, because no one can override the methods of the String
By Dineshkumar
class. So that it can provide the same features to the new String objects as well as
to the old ones.

Note: The java.lang.String class implements Serializable, Comparable and


CharSequence interfaces.
 Every time you create a string literal, the JVM checks the String constant pool.
String constant pool in Java is a pool of Strings stored in Heap memory. If the

CORE JAVA
string exists in the pool, then a reference to the existing literal is returned. If the
SringTokenizer Class
StringBuilder Class
StringBuffer Class

String Operations
string is not found, then a new instance is created and placed in the pool.
 In the below diagram, since the value of newName is also Max, new memory is

String Class
Substring

not allocated for newName and newName points to the same memory location as
name does.
String
 In the below case, name==newName will be true.
By Dineshkumar
 Strings behave a bit differently when a new instance of String class is created.
When you create a string using the new() keyword, JVM places the literal in the
constant pool and also creates a new string object in heap memory. The reference

CORE JAVA
variable points to the object in the heap memory.
SringTokenizer Class
StringBuilder Class
StringBuffer Class

String Operations
 In the below example, either 1 or 2 strings will be created. If there is already a
string literal "Welcome" in the pool, then only one string will be created. If there

String Class
Substring

is no string literal "Welcome" in the pool, then it will be first created in the pool

String
and then in the heap memory resulting in creation of 2 String objects.
 In the below case, str == newStr will be false.
By Dineshkumar
SringTokenizer Class
StringBuilder Class
StringBuffer Class
Substring
String Operations

0
H
E

1
L

2
L

3
4
O

5
String s = “HELLO JAVA”

6
String

7
A
By Dineshkumar

8
V

9
A
CORE JAVA

String Class
Method Description
char charAt(int index) It returns char value for the particular index
int length() It returns string length
static String format(String format, Object... args) It returns a formatted string.

CORE JAVA
static String format(Locale l, String format,
It returns formatted string with given locale.
SringTokenizer Class

Object... args)
StringBuilder Class
StringBuffer Class

String Operations
String substring(int beginIndex) It returns substring for given begin index.
It returns substring for given begin index and end

String Class
String substring(int beginIndex, int endIndex)
index.
Substring

It returns true or false after matching the sequence


boolean contains(CharSequence s)
of char value.

static String join(CharSequence delimiter,


It returns a joined string.
CharSequence... elements)

static String join(CharSequence delimiter,


It returns a joined string.
Iterable<? extends CharSequence> elements)
It checks the equality of string with the given
boolean equals(Object another)
object.
boolean isEmpty() It checks if string is empty.
String concat(String str) It concatenates the specified string.
It replaces all occurrences of the specified char
String replace(char old, char new)
value.
String replace(CharSequence old, CharSequence It replaces all occurrences of the specified
new) CharSequence.
Method Description
static String equalsIgnoreCase(String another) It compares another string. It doesn't check case.

String[] split(String regex) It returns a split string matching regex.

String[] split(String regex, int limit) It returns a split string matching regex and limit.

CORE JAVA
SringTokenizer Class

String intern() It returns an interned string.


StringBuilder Class
StringBuffer Class

String Operations int indexOf(int ch) It returns the specified char value index.

String Class
It returns the specified char value index starting with
Substring

int indexOf(int ch, int fromIndex)


given index.

int indexOf(String substring) It returns the specified substring index.

It returns the specified substring index starting with


int indexOf(String substring, int fromIndex)
given index.
String toLowerCase() It returns a string in lowercase.

String toLowerCase(Locale l) It returns a string in lowercase using specified locale.

String toUpperCase() It returns a string in uppercase.

String toUpperCase(Locale l) It returns a string in uppercase using specified locale.

String trim() It removes beginning and ending spaces of this string.

It converts given type into string. It is an overloaded


static String valueOf(int value)
method.
String Comparison in Java:
 We can compare String in Java on the basis of content and reference.
 It is used in authentication (by equals() method), sorting (by compareTo() method),
reference matching (by == operator) etc.

CORE JAVA
SringTokenizer Class

 There are three ways to compare String in Java:


StringBuilder Class
StringBuffer Class

String Operations
 By Using equals() Method

String Class
 The String class equals() method compares the original content of the string. It
Substring

compares values of string for equality.


 By Using == Operator String
 The == operator compares references not values.
 By compareTo() Method By Dineshkumar
 The String class compareTo() method compares values lexicographically and
returns an integer value.
 Suppose s1 and s2 are two String objects. If:
 s1 == s2 : The method returns 0.
 s1 > s2 : The method returns a positive value.
 s1 < s2 : The method returns a negative value.
CORE JAVA
String Concatenation in Java
SringTokenizer Class
StringBuilder Class

 In Java, String concatenation forms a new String that is the combination of


StringBuffer Class

String Operations
multiple strings.

String Class
Substring

 There are two ways to concatenate strings in Java:


1. By + (String concatenation) operator
Example: “Romeo” + “Juliet”
String
2. By concat() method By Dineshkumar
 The String concat() method concatenates the specified string to the end
of current string.
 Example: “Romeo”.concat(“Juliet”)
String concatenation using StringBuilder class
Example

CORE JAVA
SringTokenizer Class

StringBuilder s1 = new StringBuilder("Romeo");


StringBuilder Class
StringBuffer Class

String Operations
StringBuilder s2 = new StringBuilder(" Juliet");
StringBuilder s = s1.append(s2);

String Class
Substring

System.out.println(s.toString());

String
String concatenation using format() method
Example:
By Dineshkumar
String s1 = new String("Romeo");
String s2 = new String(" Juliet");
String s = String.format("%s%s",s1,s2);
System.out.println(s.toString());
String concatenation using String.join() method
Example

CORE JAVA
SringTokenizer Class

String s1 = new String("Romeo");


StringBuilder Class
StringBuffer Class

String Operations
String s2 = new String("Juliet");
String s = String.join(“ ",s1,s2);

String Class
Substring

System.out.println(s.toString());

String
String concatenation using StringJoiner class (Java Version 8+)
Example:
By Dineshkumar
StringJoiner s = new StringJoiner(" ");
s.add("Romeo");
s.add("Juliet");
System.out.println(s.toString());
 A part of String is called substring.
 In other words, substring is a subset of another String.

CORE JAVA
 Java String class provides the built-in substring() method that extract a substring
SringTokenizer Class
StringBuilder Class
StringBuffer Class

String Operations
from the given string by using the index values passed as an argument.
 In case of substring() method startIndex is inclusive and endIndex is exclusive.

String Class
Substring
Example: Infosys String
Substrings: In, Info, sys, fos, etc…..
By Dineshkumar
Methods:
public String substring(int startIndex)
public String substring(int startIndex, int endIndex)

Note: startIndex: inclusive, endIndex: exclusive


CORE JAVA
String.split() method:
SringTokenizer Class
StringBuilder Class
StringBuffer Class
 The split() method of String class can be used to extract a substring from a

String Operations
sentence.

String Class
Substring
 It accepts arguments in the form of a regular expression.

String
Example:
String text= new String("Java is By
one Dineshkumar
of the best");
String[] sentences = text.split(" ");
System.out.println(Arrays.toString(sentences));
 Java StringBuffer class is used to create mutable (modifiable) String objects. The
StringBuffer class in Java is the same as String class except it is mutable i.e. it can
be changed.

CORE JAVA
SringTokenizer Class

 Java StringBuffer class is thread-safe i.e. multiple threads cannot access it


StringBuilder Class

StringBuffer Class

String Operations
simultaneously. So it is safe and will result in an order.

String Class
Substring
Constructor Description

StringBuffer()
String
It creates an empty String buffer with the initial
capacity of 16.
StringBuffer(String str)
By Dineshkumar
It creates a String buffer with the specified string..
It creates an empty String buffer with the specified
StringBuffer(int capacity)
capacity as length.
Modifier and Type Method Description
public synchronized append(String s) It is used to append the specified string with this string.
StringBuffer The append() method is overloaded like append(char),
append(boolean), append(int), append(float),
append(double) etc.

CORE JAVA
public synchronized insert(int offset, String s) It is used to insert the specified string with this string at
SringTokenizer Class

StringBuffer the specified position. The insert() method is


StringBuilder Class

overloaded like insert(int, char), insert(int, boolean),

StringBuffer Class

String Operations
insert(int, int), insert(int, float), insert(int, double) etc.

String Class
Substring
public synchronized replace(int startIndex, int It is used to replace the string from specified startIndex
StringBuffer endIndex, String str) and endIndex.

public synchronized
StringBuffer
String
delete(int startIndex, int endIndex) It is used to delete the string from specified startIndex
and endIndex.

By Dineshkumar
public synchronized reverse() is used to reverse the string.
StringBuffer
public int capacity() It is used to return the current capacity.

public void ensureCapacity(int It is used to ensure the capacity at least equal to the
minimumCapacity) given minimum.
public char charAt(int index) It is used to return the character at the specified
position.
public int length() It is used to return the length of the string i.e. total
number of characters.

public String substring(int beginIndex) It is used to return the substring from the specified
beginIndex.
public String substring(int beginIndex, int It is used to return the substring from the specified
endIndex) beginIndex and endIndex.
 Java StringBuilder class is used to create mutable (modifiable) String.
 The Java StringBuilder class is same as StringBuffer class except that it is non-
synchronized.

CORE JAVA
SringTokenizer Class

 It is available since JDK 1.5.

StringBuilder Class
StringBuffer Class

String Operations
String Class
Constructor Description

Substring
It creates an empty String Builder with the initial
StringBuilder()
capacity of 16. String
StringBuilder(String str) It creates a String Builder with the specified string.

StringBuilder(int length)
By the
It creates an empty String Builder with Dineshkumar
specified
capacity as length.
Method Description

It is used to append the specified string with this string. The


public StringBuilder append(String s) append() method is overloaded like append(char),
append(boolean), append(int), append(float), append(double) etc.

CORE JAVA
SringTokenizer Class

It is used to insert the specified string with this string at the

StringBuilder Class
specified position. The insert() method is overloaded like

StringBuffer Class

String Operations
public StringBuilder insert(int offset, String s)
insert(int, char), insert(int, boolean), insert(int, int), insert(int,
float), insert(int, double) etc.

String Class
Substring
public StringBuilder replace(int startIndex, int It is used to replace the string from specified startIndex and
endIndex, String str) endIndex.

public StringBuilder delete(int startIndex, int endIndex)


endIndex. String
It is used to delete the string from specified startIndex and

public StringBuilder reverse() It is used to reverse the string.


public int capacity() By Dineshkumar
It is used to return the current capacity.
It is used to ensure the capacity at least equal to the given
public void ensureCapacity(int minimumCapacity)
minimum.

public char charAt(int index) It is used to return the character at the specified position.

It is used to return the length of the string i.e. total number of


public int length()
characters.

public String substring(int beginIndex) It is used to return the substring from the specified beginIndex.

It is used to return the substring from the specified beginIndex


public String substring(int beginIndex, int endIndex)
and endIndex.
 The java.util.StringTokenizer class allows you to break a String into tokens. It is
simple way to break a String. It is a legacy class of Java.
 It doesn't provide the facility to differentiate numbers, quoted strings,

CORE JAVA

SringTokenizer Class
identifiers etc. like StreamTokenizer class. We will discuss about the

StringBuilder Class
StringBuffer Class

String Operations
StreamTokenizer class in I/O chapter.

String Class
 In the StringTokenizer class, the delimiters can be provided at the time of

Substring
creation or one by one to the tokens.

Constructor Description
String
StringTokenizer(String str) By Dineshkumar
It creates StringTokenizer with specified string.
StringTokenizer(String str, String It creates StringTokenizer with specified string and
delim) delimiter.
It creates StringTokenizer with specified string, delimiter
StringTokenizer(String str, String and returnValue. If return value is true, delimiter
delim, boolean returnValue) characters are considered to be tokens. If it is false,
delimiter characters serve to separate tokens.
CORE JAVA

SringTokenizer Class
StringBuilder Class
StringBuffer Class

String Operations
Methods Description
boolean hasMoreTokens() It checks if there is more tokens available.

String Class
Substring
String nextToken() It returns the next token from the StringTokenizer object.
String nextToken(String delim)
boolean hasMoreElements()
It returns the next token based on the delimiter.
It is the same as hasMoreTokens() method. String
Object nextElement() It is the same as nextToken() but its return type is Object.
By Dineshkumar
int countTokens() It returns the total number of tokens.
No. String StringBuffer

CORE JAVA

SringTokenizer Class
1) The String class is immutable. The StringBuffer class is mutable.

StringBuilder Class
StringBuffer Class

String Operations
Methods Description
String is slow and consumes more memory when
StringBuffer is fast and consumes less memory
2) we concatenate
boolean hasMoreTokens() too many strings because every
It checks if there is more tokens available.

String Class
when we concatenate t strings.

Keynotes
Substring
time it creates new instance.
String nextToken() It returns the next token from the StringTokenizer object.
String nextToken(String delim) It returns the next token based on the delimiter.
String class overrides the equals() method of
boolean3)hasMoreElements() It is the
Object class. So you cansame as hasMoreTokens()
compare the contents ofmethod.
String
StringBuffer class doesn't override the equals()
method of Object class.
two strings by equals()
Object nextElement() It is themethod.
same as nextToken() but its return type is Object.
By Dineshkumar
int countTokens() It returns the total number of tokens.
String class is slower while performing StringBuffer class is faster while performing
4)
concatenation operation. concatenation operation.

5) String class uses String constant pool. StringBuffer uses Heap memory
CORE JAVA

SringTokenizer Class
StringBuilder Class
StringBuffer Class

String Operations
No. Methods StringBuffer Description StringBuilder
boolean hasMoreTokens() It checks if there is more tokens available.

String Class
Keynotes
Substring
StringBuffer is synchronized
String nextToken()
i.e. thread safe. StringBuilder is non-synchronized i.e. not thread
It returns the next token from the StringTokenizer object.
String1)nextToken(String
String
It means two threads
delim) can't the
It returns call next
the methods
token based onsafe. It means two threads can call the methods of
the delimiter.
boolean hasMoreElements() It is the same as hasMoreTokens()StringBuilder
of StringBuffer simultaneously. method. simultaneously.
Object nextElement() It is the same as nextToken() but its return type is Object.
StringBuffer is less efficient than By Dineshkumar
int countTokens()
2) It returns the total number of tokens.
StringBuilder is more efficient than StringBuffer.
StringBuilder.

3) StringBuffer was introduced in Java 1.0 StringBuilder was introduced in Java 1.5
THANK YOU

You might also like