1

I have an assignment for school, and one of the tasks is to explain a lot of tiny calculations and explaining why java gives you the output it gives you..

and one of the calculations is:

1 + '2' + 3

which for me gives a lexical error, as the teacher used the wrong "apostrophes" for my system, but I've talked to other fellow students and they told me they got an actual output, so I started reading about it, and found out that it is supposed to signify a char variable, and I also found out about the system specific types, so I changed the signs to work for my system, and now I get the answer 54..

and I cannot see the logic in it, and I've tried to google adding/calculating/math with char variables, and have found nothing that explains it well..

So I turn to you, the people of coding, that I one day might be a part of to help me understand the logic of this..

this started out as a homework assignment that I probably could have gotten through by just answering that it gives a lexical error because my compiler doesn't understand the symbol, but now it's peaked my curiosity, and I really want to know how java manages to get this answer..

thank you for any help on the matter! :)

I can see that I couldn't make a 'homework' tag, so I hope it's okay that I put it here :)

3
  • stackoverflow.com/questions/3680625/… Commented Aug 31, 2014 at 14:48
  • possible duplicate of Converting int to char in java Commented Aug 31, 2014 at 14:48
  • 1
    When you add a wider type to a narrow type, you get the wider type as a result. 1 is an int and wider than a char so you should expect an int result. Commented Aug 31, 2014 at 14:58

2 Answers 2

4

In Java, chars have a direct mapping to ints by UTF-16. For most common characters, though, casting a char value to an int yields its index on the ascii table. + isn't an operation on chars, but it is an operation for ints. Therefore, java is taking the 2 and after thinking "I can't add this", realizes it can add it if it casts it to an int. Ascii Table

As you can see in the table, '2' has a index of 50, thus 1 + 50 + 3 = 54.

Sign up to request clarification or add additional context in comments.

4 Comments

This! haha, so that's why! Thank you so much, this explains it very well! that means I'll sleep well tonight.. thank you again!
Java uses Unicode not ASCII! In string and char, it uses a Unicode encoding called UTF-16. A char is one UTF-16 code unit, one or two of which hold the value of a codepoint, which is a number that identifies a character in the character set. By design, Unicode is a superset of ASCII and uses the same codepoints for the common characters. The Character class tells whether a code unit is a complete codepoint or one of a pair for a complete codepoint.
okay; I think I understand.. so I'll mark this as the answer, as the comment clarifies why the above is sort of correct for the question I asked.
Ooo... didn't know that. Will change answer to reflect that.
0

Characters are, in fact, numbers. Computer transforms a char into an integer using a character set (charset). Charsets are tables which bind specified character to the specified integer, so your computer can transform it to binary and store it in memory. Later when needed, that number can get transformed back to character. Your compiler uses Unicode (a superset of ASCII, thanks Tom Blodget), so character '2' is actually decimal integer 50 (more info: http://unicode-table.com/en/, note that table uses hex numbers, so hex 32 is decimal 50). Since addition by default returns an integer, the character gets transformed and added, but never actually transformed back to its original form.

As for why it gives in error, it's probably because Java differs between types and your compiler (or whatever's throwing the error) is more strict. Don't take my word for that, you should probably look in the standard and your implementation. In C for example, that addition would always succeed.

Hope it clears things up a bit.

2 Comments

The compiler does not use ASCII. See my comment on this answer.
Forgot about that, thanks (and I write output strings for apps in my native language which uses sveral non-ASCII chars). Corrected the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.