Take a look at this program
Output null
nullhello
4sum
sum22
Lets take a look at the output for string s2:
The reason s2 outputs, 'nullhello' is because two key things: a) operator precedence, b) how the overloaded '+' operator works with strings.
1 the '+' operator get evaluated 1st
2 since '+' is overloaded to perform string concatenation, null is converted into a string object (see output for s1), this result is appended with "hello"
3 the assignment operator assign s2 the newly constructed string "nullhello"
also note the output for strings s3 and s4, the + operator is evaluated left to right!
so in s3, integer arithmetic is performed 1st and the sum is converted into a string when it's added to the string "sum"
the operation is done in the following order
((2+2) + "sum")->(4 + "sum")->("4" + "sum") = "4sum"
in s4 since we have a string object on the left side of the + operator the other literals are converted into a string, the operation is done in the following order
(("sum" + 2) + 2) -> ("sum2" + 2) = "sum22"
I hope this clears things up
[ May 20, 2003: Message edited by: Rajinder Yadav ]