I once decided to add two chars together, and it gave me a number. Here's the code:
class Main {
public static void main(String[] args) {
System.out.println('a'+'b');
}
}
Output: 195.
I've searched in a lot of places, but I still couldn't figure out why a char + char = int. Can someone explain this to me?
NOTE: THIS IS NOT A DUPLICATE!! The other question is asking the data type of an added char. This question asks why this happens. Those are DIFFERENT QUESTIONS WITH DIFFERENT ANSWERS!
charvariables contain a number value which represents it's position in the ASCII table. Arithmetic operations usingcharhave various behaviors depending on how it's used because acharcontains a number value but represents an character so the system will perform automatic conversions depending on the context of its use.