Pls explain.... I have exam tomorrow....šŸ˜­ | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
0

Pls explain.... I have exam tomorrow....šŸ˜­

In java, the output of : System.out. println ("Result = " + 4+2); is : Result = 42....but the output of System.out.println('c' + 4+2) is 105 (taking the ASCII value)... I don't understand the working of '+' as a concatenation operator in both cases....

10th Dec 2020, 8:04 AM
Aswin V Sivan
Aswin V Sivan - avatar
4 Respostas
+ 2
" " (double quotes) is a string. ' ' (single quotes) is a character (ascii value). If we add something to a string, it gets concatenated. If we add something to a char, it adds the number to an ascii value.
10th Dec 2020, 8:09 AM
Artem šŸ‡ŗšŸ‡¦
Artem šŸ‡ŗšŸ‡¦ - avatar
+ 2
string + int is string concatenation. The int will be converted to string. char + int is ASCII value addition. The result is an int.
10th Dec 2020, 8:16 AM
ä½ ēŸ„é“č¦å‰‡ļ¼Œęˆ‘也ę˜Æ
ä½ ēŸ„é“č¦å‰‡ļ¼Œęˆ‘也ę˜Æ - avatar
+ 1
Aswin V Sivan After string value if you try add number then it will just concatenate with string. To avoid this you can use parenthesis like: System.out.println("Result = " + (4 + 2));
10th Dec 2020, 8:38 AM
AĶ¢J
AĶ¢J - avatar
+ 1
Thank U everyone... I am saved
10th Dec 2020, 9:19 AM
Aswin V Sivan
Aswin V Sivan - avatar