• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

string issues

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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"; }
 
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your effort. It is appreciated
reply
    Bookmark Topic Watch Topic
  • New Topic