Java Reference
In-Depth Information
Assembling Strings with String Concatenation
Java allows you to assemble String objects into larger strings by using operators + or += .
This is known as string concatenation . When both operands of operator + are String ob-
jects, operator + creates a new String object in which the characters of the right operand
are placed at the end of those in the left operand—e.g., the expression "hello " + "there"
creates the String "hello there" .
In line 24 of Fig. 6.3, the expression "Maximum is: " + result uses operator + with
operands of types String and double . Every primitive value and object in Java can be rep-
resented as a String . When one of the + operator's operands is a String , the other is con-
verted to a String , then the two are concatenated . In line 24, the double value is converted
to its String representation and placed at the end of the String "Maximum is: " . If there
are any trailing zeros in a double value, these will be discarded when the number is con-
verted to a String —for example 9.3500 would be represented as 9.35.
Primitive values used in String concatenation are converted to String s. A boolean
concatenated with a String is converted to the String "true" or "false" . All objects have
a toString method that returns a String representation of the object. (We discuss toString
in more detail in subsequent chapters.) When an object is concatenated with a String , the
object's toString method is implicitly called to obtain the String representation of the
object. Method toString also can be called explicitly.
You can break large String literals into several smaller String s and place them on
multiple lines of code for readability. In this case, the String s can be reassembled using
concatenation. We discuss the details of String s in Chapter 14.
Common Programming Error 6.2
It's a syntax error to break a String literal across lines. If necessary, you can split a String
into several smaller String s and use concatenation to form the desired String .
Common Programming Error 6.3
Confusing the + operator used for string concatenation with the + operator used for addi-
tion can lead to strange results. Java evaluates the operands of an operator from left to
right. For example, if integer variable y has the value 5 , the expression "y+2="+y+2
results in the string "y + 2 = 52" , not "y + 2 = 7" , because first the value of y (5) is con-
catenated to the string "y + 2 = " , then the value 2 is concatenated to the new larger string
"y + 2 = 5" . The expression "y + 2=" + (y + 2) produces the desired result "y + 2 = 7" .
6.5 Notes on Declaring and Using Methods
There are three ways to call a method:
1. Using a method name by itself to call another method of the same class—such as
maximum(number1, number2, number3) in line 21 of Fig. 6.3.
2. Using a variable that contains a reference to an object, followed by a dot ( . ) and
the method name to call a non- static method of the referenced object—such as
the method call in line 16 of Fig. 3.2, myAccount.getName() , which calls a meth-
od of class Account from the main method of AccountTest . Non- static meth-
ods are typically called instance methods .
 
 
Search WWH ::




Custom Search