When adding 'a' + 'b'
it produces 195. Is the output datatype char
or int
?
Java Add Char to String Using + Operator This is the easiest and most straightforward way to add a character to a string in Java. We concatenate a char to the string using the + operator.
In Java, char and int are compatible types so just add them with + operator. c + x results in an integer, so you need an explicit casting to assign it to your character varaible back.
When you add a char to an int , the (p)r-value created is promoted to an int .
Size of an int is 4 bytes on most architectures, while the size of a char is 1 byte. Note that sizeof(char) is always 1 — even when CHAR_BIT == 16 or more .
The result of adding Java chars, shorts, or bytes is an int:
Java Language Specification on Binary Numeric Promotion:
- 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.
But note what it says about compound assignment operators (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.
For example:
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
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.
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'
(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')
Binary arithmetic operations on char
and byte
(and short
) promote to int
-- JLS 5.6.2.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With