Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 17

String Handling

String
• In Java a string is a sequence of characters. But, unlike some other languages
that implement strings as character arrays, Java implements strings as
objects of type String.

• String object, you are creating a string that cannot be changed. That is, once
a String object has been created,
you cannot change the characters that comprise that string.

• Each time you need an altered version of an existing string, a new String
object is created that contains the modifications. The original string is left
unchanged. This approach is used because fixed, immutable strings can be
implemented more efficiently than changeable ones.

• For those cases in which a modifiable string is desired,


Java provides two options: StringBuffer and StringBuilder. Both hold strings
that can be modified after they are created
• The java string
substring() method returns a part
of the string.
• We pass begin index and end index
number position in the java substring
method where start index is inclusive
and end index is exclusive. In other
words, start index starts from 0
whereas end index starts from 1.
• It is important to understand that the equals( ) method and the == operator
perform two different operations.
• equals( ) method compares the characters inside a String object. The == operator
compares two object references to see whether they refer to the same instance .
Java uses primitive types, such as int and char, for
performance reasons. These data types are not part of the
object hierarchy.
At times, you will need to create an object representation
for one of these primitive types.
To address this need, Java provides classes that correspond
to each of the primitive types.
In essence, these classes encapsulate, or wrap, the
primitive types within a class. Thus, they are commonly
referred to as type wrappers.
• Number has concrete subclasses that hold
explicit values of each primitive
numeric type: Double, Float, Byte, Short,
Integer, and Long.

You might also like