From the course: Java 8 Essential Training

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Building a string from multiple values

Building a string from multiple values - Java Tutorial

From the course: Java 8 Essential Training

Start my 1-month free trial

Building a string from multiple values

- When assembling strings, it's tempting to put them together using simple concatenation. I'm working in a project named StringBuilder, and I've declared two strings with values of Hello and World. Let's say I wanted to create a third string that put those strings together and added some extra characters. I could do this. I'll create my third string, and start with str1, and then I'll add a comma and a space, and then I'll append str2, and then I'll append an exclamation mark. And that all works fine. I'll output the value to the console and show that putting strings together like this will achieve the desired effect, but the problem is that, in order to do that, I've created three distinct objects. The original two and then the third. If you're dealing with a lot of string concatenation, this can cause problems in memory management, so, instead, it's recommended that you use a class named StringBuilder. This class is a member of the Java.lang package, so you don't need any special…

Contents