在Java中,两个字符相加的结果是int还是char? [英] In Java, is the result of the addition of two chars an int or a char?

查看:63
本文介绍了在Java中,两个字符相加的结果是int还是char?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当添加 'a' + 'b' 时,它产生 195.输出数据类型是 char 还是 int?

When adding 'a' + 'b' it produces 195. Is the output datatype char or int?

推荐答案

添加 Java 字符、shorts 或字节的结果是 int:

The result of adding Java chars, shorts, or bytes is an int:

二进制数字提升的 Java 语言规范:

  • 如果任何操作数是引用类型,则取消装箱转换(§5.1.8) 执行.然后:
  • 如果任一操作数是 double 类型,则other 转换为 double.
  • 否则,如果任一操作数的类型为浮动,另一个转换为浮动.
  • 否则,如果任一操作数类型为long,另一个转换为long.
  • 否则,两者都操作数被转换为 int 类型.

但请注意它对复合赋值运算符的说明(如 +=):

二元运算的结果被转换为左边变量的类型......并将转换结果存储到变量中.

The result of the binary operation is converted to the type of the left-hand variable ... and the result of the conversion is stored into the variable.

例如:

char x = 1, y = 2;
x = x + y; // compile error: "possible loss of precision (found int, required char)"
x = (char)(x + y); // explicit cast back to char; OK
x += y; // compound operation-assignment; also OK

通常可以找出结果类型的一种方法是将其强制转换为 Object 并询问它是什么类:

One way you can find out the type of the result, in general, is to cast it to an Object and ask it what class it is:

System.out.println(((Object)('a' + 'b')).getClass());
// outputs: class java.lang.Integer

如果您对性能感兴趣,请注意 Java 字节码甚至没有专门的具有较小数据类型的算术指令.比如加法,有指令iadd(对于int),ladd(对于long),fadd(对于float),dadd(双打),就是这样.为了用较小的类型模拟 x += y,编译器将使用 iadd 然后使用类似 i2c(int 到 char").如果本机 CPU 具有用于 1 字节或 2 字节数据的专用指令,则由 Java 虚拟机在运行时对其进行优化.

If you're interested in performance, note that the Java bytecode doesn't even have dedicated instructions for arithmetic with the smaller data types. For example, for adding, there are instructions iadd (for ints), ladd (for longs), fadd (for floats), dadd (for doubles), and that's it. To simulate x += y with the smaller types, the compiler will use iadd and then zero the upper bytes of the int using an instruction like i2c ("int to char"). If the native CPU has dedicated instructions for 1-byte or 2-byte data, it's up to the Java virtual machine to optimize for that at run time.

如果您想将字符连接为字符串而不是将它们解释为数字类型,有很多方法可以做到这一点.最简单的方法是在表达式中添加一个空字符串,因为添加一个字符和一个字符串会产生一个字符串.所有这些表达式的结果都是字符串 "ab":

If you want to concatenate characters as a String rather than interpreting them as a numeric type, there are lots of ways to do that. The easiest is adding an empty String to the expression, because adding a char and a String results in a String. All of these expressions result in the String "ab":

  • 'a' + "" + 'b'
  • "" + 'a' + 'b' (这是有效的,因为 "" + 'a' 首先被评估;如果 ""code> 在最后而不是你会得到 "195")
  • new String(new char[] { 'a', 'b' })
  • new StringBuilder().append('a').append('b').toString()
  • String.format("%c%c", 'a', 'b')
  • 'a' + "" + 'b'
  • "" + 'a' + 'b' (this works because "" + 'a' is evaluated first; if the "" were at the end instead you would get "195")
  • new String(new char[] { 'a', 'b' })
  • new StringBuilder().append('a').append('b').toString()
  • String.format("%c%c", 'a', 'b')

这篇关于在Java中,两个字符相加的结果是int还是char?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆