File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes string issues Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "string issues" Watch "string issues" New topic
Author

string issues

Marcus Deviln
Ranch Hand

Joined: Jan 29, 2009
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"; }


+Deviln+ The power of JAVA compels you!
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
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

Joined: Jan 29, 2009
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

Joined: May 13, 2009
Posts: 679
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

Joined: May 25, 2009
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

Joined: Jan 29, 2009
Posts: 39
Thank you for your effort. It is appreciated
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: string issues