Java Reference
In-Depth Information
System.out.print("The square of 25 is: ");
System.out.println(25 * 25);
5.2.2
The basics of String manipulation
A string literal has a class-type:
String
. Since
String
is a class, and not a prim-
itive type, the literal is contained in an instance of
String
, for example,
Activity
5-3.2
a0
String
"xy z"
String
variables can be declared and assigned values. Class
String
is special in
that there is no need to use a new-expression (although it can be used); just use
a string literal instead. For example, execution of these statements:
String s= "NO!"
String t;
t= "Yes.";
result in these variables and
String
folders:
a6
a7
s 6
String
String
t 7
"NO!"
"Yes."
We now describe various operations on strings and also introduce notation
for talking about strings.
Catenation of Strings
To
catenate
two strings means to join their characters into a single string.
The word
concatenation
is often used for this operation; we prefer the shorter
word,
catenation
. Java uses the binary infix addition symbol
+
for catenation:
Activity
5-3.3
"abc" + "xyz"
evaluates to
"abcxyz"
Of course, if both operands of
+
have numerical types, the symbol denotes addi-
tion, but if at least one operand is a
String
, the symbol denotes catenation.
If one of the operands of a catenation is a value of some primitive type, like
int
or
boolean
, it is converted to a
String
. For example, the expression
2.5 + ", 62 "
evaluates to a
String
object whose value is
" 2.5, 62 "
.
Search WWH ::
Custom Search