Guess the output for below program

public class CharPuzzle
public static void main(String args[])

I think you've guessed that the above program prints the output as AB. Why don't you try running the above program. The output for the above program will be 131.

The operator plus (+) has two forms, concatenation operator for strings and addition operator for integers. This operator works as a concatenation operation, if one of the operand is string. And it works as a arithmetic operation for numbers.

In the program above, 'A' and 'B' are two character operands for + operator. Since, string operand is not available, it works as a arithmetic operation addition i.e.; the characters will be converted into integers (Widening primitive conversion). So, the expression will be evaluated as 131 (65 + 66), please note that 65 and 66 are ASCII value for A and B respectively.

How to fix this?

The fix is very simple, just add a blank string to the expression as below