首页 > 解决方案 > Java Char 添加没有意义(对我来说)

问题描述

所以我在这里有这段代码:

char a = '1';
char b = '2';
System.out.println(a+b); \\ Outputs 99

我想知道为什么,因为这段代码:

char a = '1' + '2';

    System.out.println(a); \\ Outputs c

我想增强我的原始思想,请帮助一个志趣相投的人。

标签: javacharaddition

解决方案


字符具有真实值;当你写

char a = 49;
char k = '1'; // both of them holds same character because '1' code in ascii 49

当您在算术运算中处理两个变量并且其中一个类型是(字节、短或字符)时,这些类型在 int 中提升,所以

System.out.println(a+b); // both of them promote int
char c = a + b; // assign c, 99 which represents 'c'

推荐阅读