What does this statement print? System.out.println(5 + "bc");
uh, did you ever think of actually executing it?
It will print "5bc"
because + works both as arithmetic as well as concatenation operator. And all the expressions are executed from left to right. So in case of 2 + 3 + "bc" it first adds 2 and 3 acting as arithmetic operator and then when it finds string on one side and number on other at that time it acts as concatenation operator. So,
print(2+3+"bc") prints 5bc
print("bc"+2+3) prints bc23
It's always a good idea to read the language documentation ...
print(2+3+"bc") prints 5bc
because + works both as arithmetic as well as concatenation operator. And all the expressions are executed from left to right. So in case of 2 + 3 + "bc" it first adds 2 and 3 acting as arithmetic operator and then when it finds string on one side and number on other at that time it acts as concatenation operator. So,
print(2+3+"bc") prints 5bc
print("bc"+2+3) prints bc23
It'll print "5bc". But don't worry, the "bc" stands for "bot comedy"!
This statement will print "5bc" to the console. When concatenating a string and any other data type (in this case, the integer 5), Java converts the other data type to a string. So, in this case, the integer 5 is converted to the string "5" and then concatenated with the string "bc".
This statement will print "5bc".
To understand why, let's break down the statement:
- `System.out.println()` is a method in Java used to print text or values to the console.
- `5 + "bc"` is an addition operation between a number (`5`) and a string (`"bc"`).
- In Java, when you add a number to a string, the number is automatically converted to a string and then concatenated (or joined) with the string.
So, when you run `System.out.println(5 + "bc")`, the number `5` gets converted into a string `"5"`, and then `"5"` is concatenated with the string `"bc"`. The result is the string `"5bc"`, which is what gets printed to the console.