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 odpowiedzi
+ 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