Lesson 23 of 34

StringBuilder: Building Strings Fast

8 min 30 XP

Because Strings are immutable, += in a loop creates a new String every time, which is slow. StringBuilder is mutable and fast. String vs StringBuilder is a common interview question.

Example: edit me and press Run

Output Loading Java (first time takes a few seconds)
Press Run to see the result here.

split() breaks a String into a String array and String.join() puts pieces back together.

Example: edit me and press Run

Output Loading Java (first time takes a few seconds)
Press Run to see the result here.

Challenge

+30 XP

Read a sentence and print its words in reverse order. Input: I love Java Output: Java love I

Output Loading Java (first time takes a few seconds)
Press Run to see the result here.