2

Here's the code snippet:

public static void main (String[]arg) 
{
    char ca = 'a' ; 
    char cb = 'b' ; 
    System.out.println (ca + cb) ; 
}

The output is:

195

Why is this the case? I would think that 'a' + 'b' would be either "ab" , "12" , or 3.

Whats going on here?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
David
  • 14,569
  • 34
  • 78
  • 107
  • 1
    You've already asked 6 questions since yesterday and all about some simplest things in java. Don't you think it's worth to read some book about java basics? – Roman Mar 11 '10 at 23:50
  • 6
    i am reading a book. is asking questions bad? if asking questions is bad then this website is bad. if this website is bad then theres no impitus for you to care how many questions i ask or be here in the first place. So regardless of weather or not asking questions is bad theres no impitus to care. – David Mar 11 '10 at 23:51
  • 3
    OP put effort in his question, giving sample code and his own thought analysis, etc. It is a newbie question, but it shows a learning process that needs to be encouraged. – polygenelubricants Mar 11 '10 at 23:52
  • 2
    Setting aside the validity of the question (and I think it's fine) - that's the silliest logical proof I've ever seen, David. Thanks for the chuckle. – Michael Petrotta Mar 11 '10 at 23:53

4 Answers4

14

+ of two char is arithmetic addition, not string concatenation. You have to do something like "" + ca + cb, or use String.valueOf and Character.toString methods to ensure that at least one of the operands of + is a String for the operator to be string concatenation.

JLS 15.18 Additive Operators

If the type of either operand of a + operator is String, then the operation is string concatenation.

Otherwise, the type of each of the operands of the + operator must be a type that is convertible to a primitive numeric type, or a compile-time error occurs.

As to why you're getting 195, it's because in ASCII, 'a' = 97 and 'b' = 98, and 97 + 98 = 195.


This performs basic int and char casting.

 char ch = 'a';
 int i = (int) ch;   
 System.out.println(i);   // prints "97"
 ch = (char) 99;
 System.out.println(ch);  // prints "c"

This ignores the issue of character encoding schemes (which a beginner should not worry about... yet!).


As a note, Josh Bloch noted that it is rather unfortunate that + is overloaded for both string concatenation and integer addition ("It may have been a mistake to overload the + operator for string concatenation." -- Java Puzzlers, Puzzle 11: The Last Laugh). A lot of this kinds of confusion could've been easily avoided by having a different token for string concatenation.


See also

Community
  • 1
  • 1
polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
3

I don't speak Java, but 195 is 97 + 98 = the ASCII codes for a and b. So obviously, ca and cb are interpreted as their integer values, probably because of the + addition which does not seem to lead to a string concatenation automatically.

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
3

If you want to have a String as result of the + operator you have to use type String as operands.

You should write:

public static void main (String[]arg) 
{
    String ca = "a" ; 
    String cb = "b" ; 
    System.out.println (ca + cb) ; 
}

The + operator applied on char operands behaves as the arithmetic sum.

Dj Gaspa
  • 253
  • 1
  • 2
  • 9
1

The + operator doesn't operate over characters like it does over strings. What's happening here is that a and b are being cast to their integer ASCII codepoints - 97 and 98 - and then added together.

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179