Strings are an integral part of Java programming, used extensively for storing and manipulating text. While basic string manipulation is relatively simple, Java offers several advanced string concepts that can make your code more efficient, readable, and modern. We will explore three advanced string concepts: the join()
method, the difference between isBlank()
and isEmpty()
, and multiline strings with Text Blocks (introduced in Java 13).
join() Method
The join()
method, introduced in Java 8, provides a convenient way to concatenate strings in a collection, such as a list or an array, with a specified delimiter. It simplifies string operations and makes your code cleaner.
Syntax
1 |
String join(CharSequence delimiter, CharSequence... elements) |
- delimiter: The separator you want between each element.
- elements: The strings or elements you wish to concatenate.
Example
1 2 3 4 5 6 7 8 9 10 11 |
import java.util.Arrays; public class StringJoinExample { public static void main(String[] args) { String[] words = {"Java", "is", "awesome"}; String result = String.join(" ", words) System.out.println(result); } } //Output: Java is awesome |
In this example, the join()
method joins the elements of the array with a space " "
delimiter. This method can be very useful when you need to concatenate elements from a collection, avoiding manual loops or StringBuilder.
isBlank() vs isEmpty()
Java provides two methods—isBlank()
and isEmpty()
—to check whether a string is empty. Although they sound similar, they have distinct differences.
isEmpty()
The isEmpty()
method checks if the string has zero length. If the string is empty (i.e., its length is 0), it returns true
; otherwise, it returns false
.
isEmpty()
only returnstrue
for an empty string (""
), but not for a string with only whitespace.- Use
isEmpty()
when you need to check if a string contains any characters at all.
Syntax
1 |
boolean isEmpty() |
Example
1 2 |
String str = ""; System.out.println(str.isEmpty()); // true |
isBlank()
The isBlank()
method, introduced in Java 11, checks if a string is empty or contains only whitespace characters (spaces, tabs, or newlines).
- It returns
true
if the string is empty or contains only whitespace.isBlank()
returnstrue
for both empty strings and strings that only contain whitespace. - Use
isBlank()
when you want to check if the string is either empty or contains only whitespace characters.
Syntax
1 |
boolean isBlank() |
Example
1 2 |
String str = " "; // String with only spaces System.out.println(str.isBlank()); // true |
Multiline Strings with Text Blocks (Java 13+)
Java 13 introduced Text Blocks, which allow you to define multiline strings more easily and with improved readability. Text Blocks are enclosed in triple quotes ("""
) and allow you to write strings across multiple lines without needing to concatenate individual lines manually or escape newline characters.
Syntax
1 2 3 4 5 |
String textBlock = """ This is a multiline string that spans several lines. """; |
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public class TextBlockExample { public static void main(String[] args) { String textBlock = """ This is a simple example of a multiline string. It’s much cleaner than using escape sequences. """; System.out.println(textBlock); } } |
Output
1 2 3 4 5 |
This is a simple example of a multiline string. It’s much cleaner than using escape sequences. |