+ 4

Order of strings inside a print method.

hey, Does anyone know whats happening with this as it dosent seem to work the way i thought it would 🤔 System.out.println(("fun "+("is "+("java ")))); thanks

15th Jan 2018, 12:24 PM
D_Stark
D_Stark - avatar
4 Réponses
+ 10
Most operators including string concatenation always process from left to right. The parentheses do prioritize the operations but it's the appending to front or back make the difference. 😉 "A" + "B" not equals "B" + "A" "A" + ("B" + "C") equals ("A" + "B") + "C"
15th Jan 2018, 3:16 PM
Zephyr Koo
Zephyr Koo - avatar
+ 6
i expected to say "java is fun" i thought it might of printed in order of parenthisis 1st,2nd,3rd or is that not possible?
15th Jan 2018, 12:44 PM
D_Stark
D_Stark - avatar
+ 5
Thanks guys, yep it dosent work ☺ so i made a simple way to solve it if anyone asks the same question. String [] arr = {" fun!","is","Java "}; int i = arr.length-1; for(;i>-1;i--){ System.out.print(arr[i]);
15th Jan 2018, 4:02 PM
D_Stark
D_Stark - avatar
+ 2
I'm not sure what you expected, but you got what I expected. First, "java " is gotten. "is "+"java " is processed next yielding "is java ". "fun "+"is java " is processed last yielding "fun is java "
15th Jan 2018, 12:36 PM
John Wells
John Wells - avatar