Right arrowGo Back

Categories

String Concatenation in Java

Learn beginner-friendly ways to join strings in Java using +, concat, StringBuilder, and String.format with clear examples.

Text

Mr. Oz

Date

Read

6 mins

Level 1

Illustration representing Java string concatenation concepts
A professional headshot of a software developer in their early thirties with a friendly smile

Mr. Oz

Date

Read

6 mins

Share

Instagram logoTwitter logoYouTube logo

String concatenation means joining pieces of text to make one string. In Java, start with the + operator for quick joins, use String.concat for two strings, prefer StringBuilder in loops for speed, and use String.format for readable templates.

  1. + operator (simple and readable)

    Great for quick joins and short strings.

    String greeting = "Hello, " + name + "!";
    String fullName = first + " " + last;
  2. String.concat (when you already have two strings)

    Useful for chaining two or more existing strings.

    String result = "Hello".concat(", ").concat(name);
  3. StringBuilder (best inside loops)

    Avoids creating many temporary strings when appending repeatedly.

    StringBuilder sb = new StringBuilder();
    sb.append(first).append(" ").append(last);
    for (int i = 0; i < 3; i++) {
      sb.append("!");
    }
    String message = sb.toString();
  4. String.format (clean templates)

    Readable formatting when mixing values and text.

    String msg = String.format("%s scored %d points", name, score);
  5. String.join (combine with a delimiter)

    Great for building comma‑separated or other delimited strings.

    String csv = String.join(", ", "red", "green", "blue");

Tips and pitfalls

  • Avoid null surprises: String.valueOf(x) or Objects.toString(x, "")
  • Don’t use + inside tight loops; use StringBuilder instead.
  • Joining many values? Use String.join or streams with Collectors.joining.
  • Prefer clarity over cleverness—choose the simplest readable option.

Latest Posts

A right black arrow

Level 1

Illustration representing Java string concatenation concepts

String Concatenation in Java

Learn beginner-friendly ways to join strings in Java using +, concat, StringBuilder, and String.format with clear examples.

Text

Mr. Oz

Duration

6 mins