The characters in a
String
object are not directly available to
other objects. However, as you can see from the documentation, there are a large number of methods
that can be
used to access and manipulate those characters. For example, in an
earlier sample program
(
Listing 2
)
,
I used the
length
method to access the
number of characters stored in a
String
object as shown in the
following code fragment.
StringBuffer str6 = new StringBuffer(
"StringBuffer named str6".length());
In this case, I applied the
length
method to a literal string, but
it can be applied to any valid representation of an object of type
String
.
I then passed the value returned by the
length
method to the
constructor for a
StringBuffer
object.
As you can determine by examining the argument lists for the various methods of
the
String
class,
-
some methods return data stored in the string while
-
other methods return information about that data.
For example, the
length
method returns information about the data
stored in the
String
object.
Methods such as
charAt
and
substring
return portions of
the actual data.
Methods such
toUpperCase
can be thought of as returning the data,
but returning it in a different format.