Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!

# string issues

Marcus Deviln
Ranch Hand
Posts: 39
Suppose: x.Foo() + 42 + 44 You don't know whether this will combine or concatenate because you don't know what Foo() will return right? Why is it that Foo() + 42 + 44 will evaluate to "foo86" when you have Foo() { return "foo"; }

Fred Hamilton
Ranch Hand
Posts: 684
Marcus Deviln wrote:Suppose: x.Foo() + 42 + 44 You don't know whether this will combine or concatenate because you don't know what Foo() will return right? Why is it that Foo() + 42 + 44 will evaluate to "foo86" when you have Foo() { return "foo"; }

what does 42 + 44 + Foo() evaluate to?

Marcus Deviln
Ranch Hand
Posts: 39
Your question is: "what does 42 + 44 + Foo() evaluate to?... I thought I laid that out for you guy: "86foo" I will try to explain clearer if I can. In the output the integers are being combined in spite of the fact that the method they are being combined with returns a string. That is what is throwing me off. I had an impression of the rules whereby: In the event of a string combined with an integer you would concatenate. If you have two integers only then you will combine the two. If Foo() returns a string then shouldn't we be concatenating only in the output and not combining?

Fred Hamilton
Ranch Hand
Posts: 684
Marcus Deviln wrote:Your question is: "what does 42 + 44 + Foo() evaluate to?... I thought I laid that out for you guy: "86foo" I will try to explain clearer if I can. In the output the integers are being combined in spite of the fact that the method they are being combined with returns a string. That is what is throwing me off. I had an impression of the rules whereby: In the event of a string combined with an integer you would concatenate. If you have two integers only then you will combine the two. If Foo() returns a string then shouldn't we be concatenating only in the output and not combining?

I didn't see that in your original post. You talked about Foo() +42 + 44. I figured it was order of operation.

anyways, for me...

System.out.println( "x" + 1 +2 ); displays x12 and
System.out.println( 1 + 2 + "x" ); displays 3x

so it's left to right, and it's not clear to me why your Foo() + 42 +44 doesn't return foo4244. good luck with it

your 42 + 44 + Foo() looks right though, but again, that wasn't part of your original post.

Matthew Cox
Greenhorn
Posts: 29
Based on the information given I compiled this test code:

Output:
foo12

So to answer your question, based on the information given, you are incorrect on the output. Thus, this tells me that you are either leaving something out or somehow corrupting the output.

Marcus Deviln
Ranch Hand
Posts: 39
Thank you for your effort. It is appreciated