Char&String in Java
Char&String in Java
The Java platform contains four classes that you can use when working with character
data:
Character: A class whose instances can hold a single character value
String : A class for working with immutable (unchanging) data composed of multiple
characters.
StringBuffer: A class for storing and manipulating mutable data composed of multiple
characters
StringBuilder: A faster, drop-in replacement for StringBuffer, designed for use by a
single thread only.
Character : An object of Character type contains a single character value. You use a
Character object instead of a primitive char variable when an object is required
for example, when passing a character value into a method that changes the value or
when placing a character value into a data structure, such as an ArrayList, that requ
ires objects. *Example(CharacterDemo)
Character a = new Character('a');
Strings and String Buffers The Java platform has always provided two classes, String,
and StringBuffer, which store and manipulate strings- character data consisting of
more than one character.
The String class provides for strings whose value will not change.
The StringBuffer class provides for strings that will be modified; you use string
buffers when you know that the value of the character data will change. You typically
use string buffers for constructing character data dynamically
String buffers are safe for use in a multi-threaded environment
String builder in the same way as a string buffer, but only if it's going to be accessed by
a single thread.
A string is often created from a string literal—a series of characters enclosed in double
quotes.
You can also create String objects as you would any other Java object: using the new
keyword and a constructor. The String class provides several constructors that allow
you to provide the initial value of the string, using different sources, such as an array o
f characters, an array of bytes, a string buffer, or a string builder.
If you want to get more than one character from a string, string buffer, or string builder,
you can use the substring method.
Method Description
Returns a new string that is a substring of this string, string
String substring(int buffer, or string builder.The first integer argument specifies the
beginIndex) index of the first character. The second integer argument is the i
String substring(int ndex of the last character -1. The length of the substring is ther
beginIndex, int endIn efore the second int minus the first int. If the second integer i
dex)
s not present, the substring extends to the end of the original str
ing.
*
Methods in the String Class for Comparing Strings
Method Description
Returns true if this string ends with or begins with
boolean endsWith(String) the substring specified as an argument to the method.
boolean startsWith(String)
boolean startsWith(String, The integer argument, when present, indicates the off
int) set within the original string at which to begin looki
ng.
Compares two strings lexicographically and
returns an integer indicating whether this string is
int compareTo(String) greater than (result is > 0), equal to (result is = 0),
int compareTo(Object)**
int or less than (result is < 0) the argument. The Object
compareToIgnoreCase(String)** argument is converted to a string before the comparis
on takes place. The com-pareToIgnoreCase method i
gnores case; thus, “a” and “A” are considered equal.
Returns true if this string contains the same
boolean equals(Object) sequence of characters as the argument. The Object
boolean argument is converted to a string before the comp
equalsIgnoreCase(String) arison takes place. The equalsIgnoreCase method ig
nores case; thus, “a” and “A” are considered equal.
Manipulating Strings
Can we manipulate Strings.
The String class has several methods that appear to modify a string. Of course,
strings can't be modified, so what these methods really do is create and return a seco
nd string that contains the result.
*
Methods for Modifying a String Buffer
Method Description
StringBuffer append(boolean)
StringBuffer append(char)
StringBuffer append(char[])
StringBuffer append(char[], Appends the argument to this string buffer. The data is
int, int)
StringBuffer append(double) converted to a string before the append operation
StringBuffer append(float) takes place.
StringBuffer append(int)
StringBuffer append(long)
StringBuffer append(Object)
StringBuffer append(String)
StringBuffer delete(int
start, int end)**
StringBuffer Deletes the specified character(s) in this string buffer.
deleteCharAt(int index)**
StringBuffer insert(int
offset, boolean b)
. StringBuffer insert(int of
fset, char c)
StringBuffer insert(int offs
et, char[]str)
StringBuffer insert(int, dou
ble) Inserts the second argument into the string buffer. The
StringBuffer insert(int, flo first integer argument indicates the index before which
at) the data is to be inserted. The data is converted to a str
StringBuffer insert(int, int
ing before the insert operation takes place.
)
StringBuffer insert(int, lon
g)
StringBuffer insert(int, Obj
ect)
StringBuffer insert(int, Str
ing)
StringBuffer replace(int
start, int end, String str) Replaces the specified character(s) in this string
buffer.
StringBuffer reverse()
Reverses the sequence of characters in this string
buffer
Question 1: What is the initial capacity of the following string buffer? StringBuffer sb =
new StringBuffer("Able was I ere I saw Elba.");
Answer 1: It's the length of the initial string + 16: 26 + 16 = 42.
Question 2: Consider the following string:
String hannah = "Did Hannah see bees? Hannah did.";
Question 2a: What is the value displayed by the expression hannah.length()?
Answer 2a: 32.
Question 2b: What is the value returned by the method call hannah.charAt(12)?
Answer 2b: e.
Question 2c: Write an expression that refers to the letter b in the string referred to by
hannah.
Answer 2c: hannah.charAt(15).
Question 3: How long is the string returned by the following expression? What is the
string?
"Was it a car or a cat I saw?".substring(9, 12)
Answer 3: It's 3 characters in length: car. It does not include the space after car.
The following code fragment shows how to construct an instance of each wrapper type:
There is another way to construct any of these classes, with the exception of
Character: You can pass into the constructor a String that represents the value
to be wrapped. Most of these constructors throw NumberFormatException,
because there is always the possibility that the string will not represent a
valid value. Only Boolean does not throw this exception.