Character Array Java

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!

Jon Skeet
people
quotationmark

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]);

people

See more on this question at Stackoverflow