Incrementing Char Type In Java(Java中的递增字符类型)
问题描述
在练习 Java 时,我随机想到了这个:
While practicing Java I randomly came up with this:
class test
{
public static void main(String arg[])
{
char x='A';
x=x+1;
System.out.println(x);
}
}
我以为它会抛出一个错误,因为我们不能将数值 1
与数学中的字母 A
相加,但是下面的程序运行正确并打印
I thought it will throw an error because we can't add the numeric value 1
to the letter A
in mathematics, but the following program runs correctly and prints
B
这怎么可能?
推荐答案
在 Java 中,char
是数字类型.当您将 1
添加到 char
时,您将进入下一个 unicode 代码点.如果是 'A'
,下一个代码点是 'B'
:
In Java, char
is a numeric type. When you add 1
to a char
, you get to the next unicode code point. In case of 'A'
, the next code point is 'B'
:
char x='A';
x+=1;
System.out.println(x);
请注意,您不能使用 x=x+1
,因为它会导致隐式缩小转换.您需要改用 x++
或 x+=1
.
Note that you cannot use x=x+1
because it causes an implicit narrowing conversion. You need to use either x++
or x+=1
instead.
这篇关于Java中的递增字符类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Java中的递增字符类型


- Eclipse 的最佳 XML 编辑器 2022-01-01
- 在 Java 中,如何将 String 转换为 char 或将 char 转换 2022-01-01
- java.lang.IllegalStateException:Bean 名称“类别"的 BindingResult 和普通目标对象都不能用作请求属性 2022-01-01
- 转换 ldap 日期 2022-01-01
- GC_FOR_ALLOC 是否更“严重"?在调查内存使用情况时? 2022-01-01
- 未找到/usr/local/lib 中的库 2022-01-01
- 如何指定 CORS 的响应标头? 2022-01-01
- 获取数字的最后一位 2022-01-01
- 如何使 JFrame 背景和 JPanel 透明且仅显示图像 2022-01-01
- 将 Java Swing 桌面应用程序国际化的最佳实践是什么? 2022-01-01