The String class is used for text manipulation. As you read, you will learn different ways to create Strings, methods to manipulate Strings, the String concatenation operator '+', and about how Strings are immutable.
11. + Operator
Answer:
Yes: in output statements like this:
System.out.println( "Result is:" + result );
The +
operator is a short way of asking for concatenation. (If result
is a number,
it is first converted into characters before the concatenation is done.)
+ Operator
Here the +
operator is used instead of using the concat()
method:
String first = "Red " ;
String last = "Rose" ;
String name = first + last ;
String concatenation, done by concat()
or by +
, always constructs a new object based on data in other objects. Those objects are not altered at all.
When the operands on either side of +
are numbers, then +
means "addition". If one or both of the operands is a String
reference, then String
concatenation is
performed. When an operator such as +
changes meaning depending on its arguments, it is said to be overloaded.
Question 11:
Say that the following statement is added after the others:
String reversed = last + first;
Does it changefirst
,last
, orname
?