How i print xy with using char data type | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How i print xy with using char data type

char a = 'x'; char b = 'y'; char c = a+b;

13th Feb 2018, 6:21 AM
Prashant Kumar
Prashant Kumar - avatar
8 Answers
+ 9
char data type can only store a single character at a time. Therefore if you want to concatenate/combine multiple characters together, you'll need a string. The simplest way would be something like this:- String c = new String(new char[] { a, b }); and I hope you do notice that string is actually an array of characters. 😉
13th Feb 2018, 6:28 AM
Zephyr Koo
Zephyr Koo - avatar
+ 1
char[] c = {a, b}; Or String c = a + b; It's the same
13th Feb 2018, 6:23 AM
Leonardo Medai Cossutta
Leonardo Medai Cossutta - avatar
+ 1
is it not possible to add with using char ?
13th Feb 2018, 6:25 AM
Prashant Kumar
Prashant Kumar - avatar
+ 1
More than one char became a String, so not
13th Feb 2018, 6:28 AM
Leonardo Medai Cossutta
Leonardo Medai Cossutta - avatar
+ 1
ok thanx , I was thinking may be it is possible to add only single character with char
13th Feb 2018, 6:33 AM
Prashant Kumar
Prashant Kumar - avatar
+ 1
You can cast the chars to int (get the ASCII code) and then sum them and convert the result back to char. But i think the result of this operation isn't what you want
13th Feb 2018, 6:39 AM
Leonardo Medai Cossutta
Leonardo Medai Cossutta - avatar
+ 1
thank you
13th Feb 2018, 6:40 AM
Prashant Kumar
Prashant Kumar - avatar
+ 1
Here it is the result of the sum with integer cast if you are Interested https://code.sololearn.com/cPvjP4dZZhc4/?ref=app
13th Feb 2018, 6:44 AM
Leonardo Medai Cossutta
Leonardo Medai Cossutta - avatar