フリー問題

Java SE 21 Developer Professional のフリー問題 2 / 20 問目

問題文

A method builds one line of text by concatenating a few thousand short pieces inside a loop. Readability is not a concern here but the time spent is. What is the best choice?

選択肢

  1. Collect the pieces into a list and join them at the end with the method of String that takes a separator, and that avoids creating any intermediate text at all.
  2. Concatenate with the plus operator, and the compiler optimizes it away while it builds each line.
  3. Append the pieces to one StringBuilder inside the loop and convert it to a String once after the loop has finished.
  4. Concatenate into a String but declare the variable as final so the compiler keeps the value in a register, so the loop avoids extra garbage.

解答・解説を確認するには

正解と解説の確認、回答の記録には無料登録が必要です。登録すると演習モードでフリー問題に回答し、正誤と解説をその場で確認できます。