-
A
String
literal is a sequence of zero or more
characters
enclosed
within double quotation marks. A
String
object is a sequence of zero or more characters, plus a variety of class and instance
methods
and
variables
.
-
A
String
object is created automatically by Java the first time it encounters a
literal string
, such as
"
Socrates
"
, in a program. Subsequent occurrences of the literal do not cause additional objects to be
instantiated
. Instead, every occurrence of the literal
"
Socrates
"
refers to the initial object.
-
A
String
object is created whenever the
new
operator is used in conjunction with a
String()
constructorfor example,
new String("hello")
.
-
The
String
concatenation operator is the overloaded + symbol; it is used to combine two
String
s into a single
String
:
"
hello
"
+
"
world
" ==> "
helloworld
"
.
-
String
s are indexed starting at 0. The
indexOf()
and
lastIndexOf()
methods are used for finding the first or last occurrence of a character or substring within a
String
. The
valueOf()
methods convert a nonstring into a
String
. The
length()
method determines the number of characters in a
String
. The
charAt()
method returns the single character at a particular index position. The various
substring()
methods return the substring at particular index
positions
in a
String
.
-
The overloaded
equals()
method returns
true
if two
String
s contain the same exact sequence of characters. The
==
operator, when used on
String
s, returns true if two references
designate
the same
String
object.
-
[Page 341]
-
String
objects are
immutable
. They cannot be modified.
-
A
StringBuffer
is a string object that can be modified using methods such as
insert()
and
append()
.
-
A
StringTokenizer
is an object that can be used to break a
String
into a collection of
tokens
separated by
delimiters
. The white space characterstabs, blanks, and new linesare the default delimiters.
-
The
FontMetrics
class is used to obtain the specific dimensions of the various
Font
s. It is useful when you wish to center text.
Font
s are
inherently
platform dependent. For maximum portability, it is best to use default fonts.