java - 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
我想增强我的原始思想,请帮助一个志趣相投的人。
解决方案
字符具有真实值;当你写
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'
推荐阅读
- ansible - Ansible - 创建复杂变量
- jquery - 动态添加的复选框未触发 onCheck 或 onUncheck 事件
- r - 如何使用 R 上传/下载 Googledrive 包?
- vba - 使用 VBA 在 MS Access 查询文本数据类型字段中按金额过滤
- laravel - 删除链接而不是按钮 laravel
- java - Spring 循环依赖,即使使用通过 setter 注入
- graphql - GraphQL 数据存储在哪里?
- python - 为什么链接列表上的索引匹配失败?
- ios - 如何将对象传递给应该快速确认两个协议的函数?
- c# - 添加 NuGet 包后 Clickonce 部署大小膨胀