Java Reference
In-Depth Information
• WHAT YOU LEARNED IN THIS CHAPTER
TOPIC
CONCEPT
Using an Array
You use an array to hold multiple values of the same type, identified through a single variable
name.
Accessing Array
Elements
You reference an individual element of an array by using an index value of type
int
. The index
value for an array element is the offset of that element from the first element in the array, so the in-
dex of the first element is 0.
Using Array Ele-
ments
An array element can be used in the same way as a single variable of the same type.
The Number of
Array Elements
You can obtain the number of elements in an array by using the
length
member of the array object.
Arrays of Arrays An array element can also contain an array, so you can define arrays of arrays, or arrays of arrays of
arrays, and so on.
String
Objects
A
String
object stores a fixed character string that cannot be changed. However, you can assign a
given
String
variable to a different
String
object.
String Length
You can obtain the number of characters stored in a
String
object by using the
length()
method
for the object.
String
Class
Methods
The
String
class provides methods for joining, searching, and modifying strings — the modifica-
tions being achieved by creating a new
String
object.
Mutable Strings
StringBuffer
and
StringBuilder
objects can store a string of characters that you can modify.
StringBuffer
and
StringBuilder
objects support the same set of operations.
StringBuffer
ob-
jects are safe when accessed by multiple threads of execution whereas
StringBuilder
object are
not.
StringBuffer
and
StringBuilder
Objects
Length and Capa-
city of a
StringBuffer
object
You can get the number of characters stored in a
StringBuffer
object by calling its
length()
method, and you can find out the current maximum number of characters it can store by using its
capacity()
method. You can change both the length and the capacity for a
StringBuffer
object.
Creating a
String
Object from a
StringBuffer
object
You can create a
String
object from a
StringBuffer
object by using the
toString()
method of
the
StringBuffer
object.