Search code examples
javaaddition

Adding two numbers that were declared as char type in java how it is posible?


If the single quotes are removed in the declaration it is showing 11

class Sample
{
    public static void main(String ar[])
    {
         char v1='5';
         char v2='6';
         System.out.println(v1 + v2);
    }
}

Output: 107


Solution

  • In Java, a char is a 16-bit, unsigned integral type. When used in arithmetic operations, they are "promoted" to int operands with the same value.

    The Unicode character '5' has a value of 53, and '6' has a value of 54. Add them together, and you have 107.