Why this is so? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why this is so?

int x= 8; System.out.print(++x*3+""+x); Why this is equal to 279?

28th Aug 2019, 10:29 AM
Самуил Георгиев
Самуил Георгиев - avatar
5 Answers
+ 1
This equation has four main parts. Firstly, ++x. This increments x by one and evaluates it, so that part is equal to 9 (8+1). Second, the 9 is multiplied by 3; this is equal to 27. Third, you add an empty string (+""). This converts an output into text form, not number form. So now the output is still 27, but as a string. Lastly, you simply add x, which became equal to 9 from the first part. Since the output is a string, it simply adds "9" to the end of "27". As a result, the output is 279.
28th Aug 2019, 10:40 AM
Daniel C
Daniel C - avatar
+ 1
No, the two numbers are just joined. Adding an empty string to a number will make it a string, and adding a number to a string just inserts it to the end, so 27 concatinated with 9 = 279
28th Aug 2019, 10:40 AM
Airree
Airree - avatar
0
++x * 3 = 27 x = 9 27 + "" + 9 = 279
28th Aug 2019, 10:36 AM
Airree
Airree - avatar
0
But why ""=243?
28th Aug 2019, 10:38 AM
Самуил Георгиев
Самуил Георгиев - avatar
0
Yes, I understand
28th Aug 2019, 10:43 AM
Самуил Георгиев
Самуил Георгиев - avatar