• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why concatenation not addition?  RSS feed

 
Peter Park
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Newb here,

System.out.println("bc" + 2 + 3); prints: bc23

why is it bc23 not bc5?

Explanation please.

Thank you!
 
Ganesh Patekar
Bartender
Posts: 726
23
Eclipse IDE Hibernate jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to CodeRanch!
Peter Park wrote:why is it bc23 not bc5? 
Because of String concatenation.

If the type of either operand of a + operator is String, then the operation is string concatenation. So in your example there is String "bc" which causes String concatenation.

Execution of expression "bc" + 2 + 3 starts from left side so "bc" + 2 results "bc2" then again String concatenation between "bc2" + 3 results "bc23"

Edit: to get bc5 as output, you need to get this 2 + 3 part executed before "bc" + so see If you can figure It out?
 
Peter Park
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Would it be System.out.println((2+3) + "bc");

Thanks for the help!
 
Peter Park
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Would it be System.out.println("bc" + (2+3) );

Thanks for the help!
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Peter Park wrote:Would it be System.out.println((2+3) + "bc"); . . .
No. That will print 5bc and you want bc5.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Peter Park wrote:Would it be System.out.println("bc" + (2+3) ); . . .
That is probably the simplest way to achieve what you want, yes.

And welcome to the Ranch again
I have messed around with your posts because I misread one. Sorry. Have removed the quote tags as unnecessary.
 
Peter Park
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Haha thank you!

Out of context but how does one edit his or her posts/replies?

I can't seem to do it for myself.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't. The system may change at short notice, but we have had problems with people editing posts after they were replied to and we severely restrict editing. If you need something editing, ask a mod.
 
Anton Golovin
Ranch Hand
Posts: 531
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Peter Park wrote:Newb here,

System.out.println("bc" + 2 + 3); prints: bc23

why is it bc23 not bc5?

Explanation please.

Thank you!


Hi, Peter, it evalutes the plus operator from left to right in order. The fact that it is overloaded for String makes no difference to that order, so it basically just converts 2 to String, so you get bc2, then it converts 3 to String, so you get bc23.

With best regards,

Anton.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!