Java Reference
In-Depth Information
Arrays and Strings
In this chapter you will start to use Java objects. You will first be introduced to arrays, which enable you
to deal with a number of variables of the same type through a single variable name, and then you will
look at how to handle character strings. By the end of this chapter you will have learned:
What arrays are and how you declare and initialize them.
❑
How you access individual elements of an array.
❑
How you can use individual elements of an array.
❑
How to declare arrays of arrays.
❑
How you can create arrays of arrays with different lengths.
❑
How to create
String
objects.
❑
How to create and use arrays of
String
objects.
❑
What operations are available for
String
objects.
❑
What
StringBuffer
objects are and how they relate to operations on
String
objects.
❑
What operations are available for
StringBuffer
objects.
❑
Some of what we discuss in this chapter relates to objects, and as we have not yet covered in detail how a
class (or object definition) is defined we will have to skate over some points, but all will be revealed in
Chapter 5.
Arrays
With the basic built-in Java data types we have seen in the previous chapters, each identifier corresponds to a
single variable. But when you want to handle sets of values of the same type - the first 1000 primes for
example - you really don't want to have to name them individually. What you need is an
array
.