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

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

问题描述

添加'a'+'b'时会生成195.输出数据类型 char int

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

推荐答案

添加Java字符,短片或字节的结果是 int

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

关于二进制数值推广的Java语言规范



  • 如果任何操作数是引用类型,则执行解包装转换
    (§5.1.8)。然后:

  • 如果任一操作数的类型为double,则
    other将转换为双精度。

  • 否则,类型
    float,另一个转换为float。

  • 否则,如果操作数
    的类型为long,
  • 否则,
    操作数都会转换为int类型。

  • If any of the operands is of a reference type, unboxing conversion (§5.1.8) is performed. Then:
  • If either operand is of type double, the other is converted to double.
  • Otherwise, if either operand is of type float, the other is converted to float.
  • Otherwise, if either operand is of type long, the other is converted to long.
  • Otherwise, both operands are converted to type int.

但请注意它关于复合赋值运算符的说明like + =)


二进制操作的结果转换为左侧变量的类型。 ,并将转换结果存储到变量中。

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

一种可以找出结果类型的方法,一般来说,对象并询问它是什么类:

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 (对于longs), fadd (对于浮动), dadd (对于双精度),就是这样。要使用较小的类型来模拟 x + = y ,编译器将使用 iadd int使用 i2c (int to 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.

如果你想要为了将字符串连接成字符串而不是将它们解释为数字类型,有很多方法来做到这一点。最简单的是向表达式中添加一个空字符串,因为添加一个字符串和一个字符串会产生一个字符串。所有这些表达式导致String 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'首先被评估;如果结束而是你会得到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天全站免登陆