Javatpoint Logo
Javatpoint Logo

String Interpolation in Java

Java is a versatile and widely-used programming language known for its robustness and platform independence. It offers the various ways to manipulate the strings, and one powerful feature is that string interpolation. String interpolation allows us to embed variables and the expressions directly into the string literals, making it easier to create formatted and the dynamic strings. In this section, we will discuss string interpolation in Java, covering its syntax, use cases, and the practical examples.

String Interpolation

String interpolation is a technique used to embed variables, expressions, or method calls within a string literal. It simplifies the process of creating dynamic strings by allowing you to insert the values directly into the string without the need for explicit concatenation. Java introduced a convenient way to achieve string interpolation starting from Java 15.

String Interpolation Syntax

In Java, we can use the 'text blocks' feature along with the '$' character to achieve the string interpolation. Here's the basic syntax:

Let's break down the syntax:

  • Enclose the interpolated string within the triple double-quotes (""").
  • Inside the string, use ${} to enclose the variable, expression, or method call that you want to interpolate.

Use Cases for String Interpolation

String interpolation can be useful in various scenarios, including:

1. Building Log Messages:

We can create the informative log messages by embedding variables that holds the relevant data, such as timestamps, error codes, or the user names.

2. Generating SQL Queries:

When constructing SQL queries, we can interpolate variables representing table names, column names, or user input to create the dynamic queries.

3. Creating User-Friendly Messages:

Customize messages to users by embedding user-specific information, such as their username or account balance, into messages.

4. Formatting Strings:

Easily format strings by interpolating expressions that perform calculations, date formatting, or other operations.

Let's explore some practical examples to understand how string interpolation works in Java.

Example 1: Interpolating Variables


Hello, Alice!
You are 30 years old.

In this example, we interpolate the values of the name and age variables into the message string. The resulting string displays the values of these variables when printed.

Example 2: Interpolating Expressions


The sum of 10 and 20 is 30.
The product of 10 and 20 is 200.

In this example, we interpolate expressions that calculate the sum and product of num1 and num2. The interpolated values are calculated and included in the resultMessage string.

Example 3: Interpolating Method Calls


Hello, Bob!
Length of your name is 3 characters

In this example, we interpolate a method call to capitalize(name) and name.length() within the message string. The method capitalize capitalizes the first letter of the name variable.

In Summary, String interpolation in Java, introduced in Java 15, provides a convenient way to create dynamic and formatted strings by embedding variables, expressions, and method calls within string literals.

The feature simplifies the process of string manipulation and enhances the readability of code. By leveraging string interpolation, you can make your Java programs more expressive and user-friendly. As we continue to explore Java, consider incorporating string interpolation into your toolkit for improved string handling and enhanced code clarity. It's a valuable addition to the Java language that can streamline development process and make code more maintainable.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA