1

This question is hard to be asked on Google even though is so simple. Basically I wrote this :

      public static void main(String[] args) {
         char cipher[] = {'a','b','c','c','d','t','w'};
           System.out.println(cipher[0]+cipher[2]);
          }
 }

and the println result was : 196 instead of ac. Of course when I did

 System.out.println(cipher[0]+""+cipher[2]);

It showed me ac as intended. So my question is just what is this 196 ? Thanks!

4 Answers 4

5

So my question is just what is this 196 ?

It's the UTF-16 code unit for 'a' (which is 97) followed by the UTF-16 code unit for 'c' (which is 99).

Other than for string concatenation, operands of the addition operator undergo binary numeric promotion (JLS 5.6.2) so you're actually performing addition of int values. Your code is equivalent to:

System.out.println((int) cipher[0] + (int) cipher[2]);
Sign up to request clarification or add additional context in comments.

3 Comments

Beat me by 10 seconds. Anyways... I'm not too disappointed of being beaten by a 617k guy ;)
+1 a char is an unsigned 16-bit integer and when you add two of them together with + you get an int type value.
@Izmaki a 617k guy !? do you mean there is more than one ;)
0

196 is the ASCII value of 'a' + the ASCII value of 'c'.

When you add chars together, without any other hints, Java interprets them as numbers.

Comments

0

In Java, a char is essentially an unsigned 16-bit integer with their integer value corresponding to their Unicode value. 196 is the sum of the integer representations of 'a' ja 'c'.

Comments

0

The result 196 is the ASCII value de 'a' (ASCII 97) + 'c' (ASCII 99).

Comments

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.