Computer Applications
Give the output of the following program segment:
System.out.println("nine:" + 5 + 4);
System.out.println("nine:" + (5 + 4));
Answer
"nine:54"
"nine:9"
Working
- First "nine:" + 5 is evaluated. As one operand of addition operator is string so implicit conversion converts 5 to string and concatenates it with "nine:" and the result is "nine:5". Next "nine:5" + 4 is evaluated and similarly it results in "nine:54".
- Due to brackets, first (5 + 4) is evaluated. As both operands are numeric so arithmetic addition takes place resulting in 9. Next "nine:" + 9 is evaluated and results in "nine:9".