1) System.out.print(12 * 100 + 34)
This outputs 1234 because it is outputting what is inside the parenthesis. So, 12 * 100 = 1200 1200 + 34 = 1234
2) System.out.print("12" + 34)
The "x" tells the compiler to output the value that is enclosed within the quotations. so the compiler will automatically output 12 as the first value because it is the first listed. Then 34 is the next output because nothing is being added to 34. 12 isnt being added because it has already been outputted because it is within the quotations. so 12 then 34 [1234]
3) System.out.print(12 + "34")
This is similar to the 2nd output. it outputs only 12 because nothing is being added to it because 34 is within the quotations. since 12 is listed first, 12 is output with 34 following. 12 and 34 [1234]
Hi Ryan, good explanation here, just one item of follow-up, consider:
ReplyDeleteSystem.out.print("12" + 34)
The '+' operation, as you noted, is not performing traditional addition, but there is a term that specifically describes what's happening here. What is that term?
Thank You.
concatenate
ReplyDeleteMost excellent, 15 XPs awarded for this post.
ReplyDelete