Equality ( == ) and inequality ( != ) can be used with operands of type
boolean . For example, true == true evaluates to true and true != true eval-
uates to false .
Type boolean is covered in detail in Sec. 6.7. Look especially at the mate-
rial on the marks of a boolean tyro at the end of Sec. 6.7.
Type String is different from the previous three types we introduced, in a way
that will be explained in Section 1.3.4. For the moment, we need just to be able
to write some String values and catenate them together.
"This is a String"
consists of a String literal : a sequence of characters enclosed in double-quote
marks " . The value of a String literal is simply the sequence of characters. You
can catenate two String values to yield the sequence of characters in the first
String followed by the sequence of characters in the second String using oper-
ator + . Thus, the expression
"First part" + " Second part"
has the value "First part Second part" .
Operator + is overloaded ; it is used for addition of two int s, for addition of
two double s, and for catenation of two String s.
If one operand of + is a String and the other is not, the other one is con-
verted to a String before the catenation takes place. Thus, the expression
"123" + 61
has the value " 12361 " . Be careful when you use this feature. The two expres-
"one" +5+2 and "one" + (5 + 2)
have different values. (Type these expressions into your IDE and evaluate them.)
In the first one, the catenation of "one" and 5 yields "one5" , and the catenation
of this value with 2 yields "one52" . In the second expression, because of the
parentheses, the integer addition is done first and then the conversion to a String
is performed, with the result "one7" .
How does one get the double-quote character itself in a String ? By preced-
ing it by the escape character '\' . For example, the literal "a\"b" consists of
three characters: a , " , and b . But this means that the backslash cannot be used to
represent itself. Instead, use \\ to represent the backslash. For example, the lit-
eral "a\\b" consists of three characters: a , \ , and b .