+ 4

Please explain what's happening in the code

https://code.sololearn.com/cU1oFgaQPXA8/?ref=app

7th Oct 2021, 11:10 AM
No Mercy
No Mercy - avatar
3 Réponses
+ 11
""+b+a will be converted into string, .toString() or ""+ before variable or value converts value into the string and a+b will be added because they are not converted into string. a+b+""+b+a 3+1""+1+3 4 1 3 Hence output will be 4 1 3. For example : class Main { public static void main(String[] args) { // create double variables double num1 = 347.6D; double num2 = 86.56D; // convert double to string // using + sign String str1 = "" + num1; String str2 = "" + num2; // print string variables System.out.println(str1); // 347.6 System.out.println(str2); // 86.56 } } Notice the line, String str1 = "" + num1; where ""+ convert variable to string and print variables in string.
7th Oct 2021, 11:43 AM
Sujal
Sujal - avatar
+ 6
If you add a string and a number the number becomes a string. So let's cut this into pieces. a = 3 b = 1 a+b+""+b+a 3+1 = 4 //<- these are two numbers then 4 + "" // <- number and string "4" + 1 // <- string and number "41" + 3 // <- string and number "413" I hope it's now more clear what's happening. 😉
7th Oct 2021, 11:30 AM
Stefanoo
Stefanoo - avatar
+ 2
a = 3; b = 1; a+b = 4; So the first word is 4. You have a quotes so it turns to String type. So the second word is 1, because the value of b is 1. And the third word is 3, because the value of a is 3.
7th Oct 2021, 12:25 PM
Yee Hoe