• 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

Addition or Concatenation??

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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,
 
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic