0% found this document useful (0 votes)
59 views25 pages

Java String Class Methods Explained

The document discusses the String class in Java, which allows the creation of objects to hold string values and provides various methods for manipulating strings, such as length(), charAt(), concat(), substring(), equals(), and contains(). It explains how string literals are stored in the string constant pool and covers examples and details of several common String class methods.

Uploaded by

shabu lalo
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)
59 views25 pages

Java String Class Methods Explained

The document discusses the String class in Java, which allows the creation of objects to hold string values and provides various methods for manipulating strings, such as length(), charAt(), concat(), substring(), equals(), and contains(). It explains how string literals are stored in the string constant pool and covers examples and details of several common String class methods.

Uploaded by

shabu lalo
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

COMPROG1

Program Logic
Formulation
using JAVA
LESSON 3-1

ROMMEL L. DORIN
Contents

String class
String
String Class
▪ The String class allows you to create
objects for holding strings. It also has
various methods that allow you to work
with strings.
Introduction
The String class is one of the classes which
implement this interface. Hence, string is basically
an object that represents a sequence of char
values.

We have different String methods such as concat(),


length(), compareTo(), equals(), etc. to handle
various string operations.
Introduction
There exists a primitive data type char in Java
which is used to declare character-type variables. It
represents or stores a single character. But what if
we need to store any name or a sequence of
characters?
String
String Class Methods
String
String Class Methods
Introduction
A literal, in computer science, is a notation used for
representing a value. Java String literal can be
created and represented using the double-quotes.
All of the content/characters can be added in
between the double quotes.
Introduction
The string constant pool in Java is a storage area in the
heap memory that stores string literals. When a string is
created, the JVM checks if the same value exists in the
string pool. If it does, the reference to that existing object is
returned. Otherwise, a new string object is created and
added to the string pool, and its reference is returned.
Introduction
The string constant pool in Java is a storage area in the
heap memory that stores string literals. When a string is
created, the JVM checks if the same value exists in the
string pool. If it does, the reference to that existing object is
returned. Otherwise, a new string object is created and
added to the string pool, and its reference is returned.
Introduction
Strings can be created using the new keyword in Java. When a string is
created with new, a new object of the String class is created in the heap
memory, outside the string constant pool. Unlike string literals, these
objects are allocated separate memory space in the heap, regardless of
whether the same value already exists in the heap or not.
Introduction
Examples
Methods of Strings

1. int length() Method


The length() method of
string in Java is used to get
the length of the string.
Methods of Strings

2. char charAt(int index)


Method
The charAt() method of string
in Java accepts an index
number as an argument and
returns the character at the
given index number.
Methods of Strings

3. String concat(String
string1) Method

The concat() method of


string in Java is used for
concatenating two
strings.
Methods of Strings
4. String substring(int beginIndex, int
endIndex[optional]) method

Details: The substring() method in Java returns a


specified part of a string. It takes two parameters: the
starting index (inclusive) and the ending index
(exclusive). The substring will include characters starting
from the beginIndex up to endIndex - 1.

If we do not provide the endIndex, it is assumed to be


the length of the string.
Methods of Strings
String start at index 0

Index 0 = “s”

Total characters: 13

Valid index: 0-13


Methods of Strings
5. String equals(String
anotherString) method
The equals() method of string in
Java is used to verify if both the
strings are equal or not. The
equals method accepts another
string as an argument and then
checks for the equality of both the
strings. If both the strings are
equal, true is returned else false is
returned.
Methods of Strings
6. String contains(String
substring) Method
The contains() method in
Java is used to check if a
string contains a specified
substring. It returns true if
the substring is found
and false otherwise.
Methods of Strings
Methods of Strings
7. String join()
Details: The join() method
of string in Java as the
name suggests is used to
join a group of strings
using the joiner between
them. The joiner variable
can be any character,
string or a sequence of
characters.
Methods of Strings
Methods of Strings

You might also like