Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Addition or Concatenation??

 
Piyush Sam
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What will be output of the following program?

System.out.println('A');
System.out.println('A' + 'B');

1 A AB
2 65 131
3 A 131

Anser is A 131

The problem is Java's design blunder of using + to mean both addition and concatenation. Addition also promotes to int, which println displays differently from char.

Plz somebody explain in more detail.


Regards,
 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sam
When you do a addition operation on a char it gets automatically casted to integers , so it will be a normal addition.

Ex : System.out.println ( "One" + "Two" + 1 + 2 ) = OneTwo12
S.O.P ( 1 + 2 ) = 3 // and not 12.


The problem is Java's design blunder of using +
You should not blame the design of java , when the concepts are not clear.
 
Steve Morrow
Ranch Hand
Posts: 657
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Plz somebody explain in more detail.


Please make the extra effort to write out words such as "please". The extra keystrokes won't cost much in the way of time, and the enhanced clarity will be appreciated by those communicating on a forum with international readership.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic