+ 5

What happens first : Concatenation or Arithmetic Operation in Java.

System.out.print("Hello"+1+2); //output:Hello12 System.out.print("Hello"+1-2); //output:Error System.out.print("Hello"+1*2); //output:Hello2 System.out.print("Hello"+1/2); //output:Hello0

31st Jul 2019, 11:11 AM
Prathvi
Prathvi - avatar
4 odpowiedzi
+ 1
well you could say it is just Pemdas here.
31st Jul 2019, 12:02 PM
Brave Tea
Brave Tea - avatar
+ 1
java can concatenate strings but also primitive types (such as the integer 1) it looks at pemdas as well. so first example: it’s all plusses, so pembas goes from left to right. first concatenating the string and the 1 making a new string Hello1 that new string is then concatenated with 2 forming Hello12 example 2 + and - are worth the same so goes from left to right. Hello1 is formed again, but you can’t do - on a string so error example 3 * is worth more than + so first it does 1*2 == 2 and then does + which concatenates the string and the new number forming Hello2 example 4 / is worth more than + so first goes 1/2 which in ints == 0 then it concatenates with the + and form Hello0 do look up Pemdas as well please :)
31st Jul 2019, 12:42 PM
Brave Tea
Brave Tea - avatar
0
sorry!! pemDas! but google it, please. it is the order of operations and it is quite important to know :)
31st Jul 2019, 1:47 PM
Brave Tea
Brave Tea - avatar