String Interpolation in Java
Last Updated :
19 Nov, 2024
String Interpolation is a process in which the placeholder characters are replaced with the variables (or strings in this case) which allow to dynamically or efficiently print out text output. This makes the code more compact and avoids repetition of using variables to print the output. String Interpolation replaces the placeholder with the mentioned variable names assigned to strings and hence makes it efficient to write large variable names or text.
String Interpolation in Java can be done in several ways with some concatenation operator or built-in functions or classes. In this article, we will learn String Interpolation in Java.
Example:
Input:
String a = "Geeks for Geeks";
String b = "coding";
// Print and display the string Interpolated
System.out.println(a + " is the best platform to learn " + b);
Output:
Geeks for Geeks is the best platform to learn coding
Program:
Below is the simplest way to do string interpolation using "+"
operator in Java. This operator is used to concatenate strings by placing variables or string values before or after it by replacing the variables with their actual values.
Java
// Java Program to Illustrate String Interpolation using '+' Operator
import java.io.*;
class GFG {
public static void main(String[] args) {
String a = "Geeks for Geeks";
String b = "coding";
// Print and display the string Interpolated
System.out.println(
a + " is the best platform to learn "
+ b);
}
}
OutputGeeks for Geeks is the best platform to learn coding
Some other ways to perform string interpolation in Java are as follows:
The String format() method in Java uses placeholders, for example, "%s"
to insert variable values into a string, with the variables provided as additional arguments after the format string.
Example:
Java
// Java Program to Illustrate String Interpolation using format() method
class GFG {
public static void main(String[] args) {
String a = "Geeks for Geeks";
String b = "coding";
// Print and display the interpolated string
System.out.println(String.format(
"%s is the best platform to learn %s", a,
b));
}
}
OutputGeeks for Geeks is the best platform to learn coding
In this method, we have to import the MessgeFormat class which uses indexed placeholders, for example, "{0}, {1}" for formatting by offering advantages like avoiding repetition of the same variable.
Example:
Java
// Java Program to Illustrate String Interpolation using MessageFormat class
import java.io.*;
// Importing MessageFormat class from java.text package
import java.text.MessageFormat;
class GFG {
public static void main(String[] args) {
String a = "Democracy";
String b = "people";
// Print and display the interpolated string
System.out.println(MessageFormat.format(
"{0} is a government of the {1}, for the {1} and by the {1}.",
a, b));
}
}
OutputDemocracy is a government of the people, for the people and by the people.
Using StringBuilder Class
This method is quite lengthy and not very commonly used for the same reason. We use the StringBuilder class to instantiate a new object and call the append function to add in the variable before the formatted text in the append function. There could be as many as append functions chained up in the StringBuilder class.
Example:
Java
// Java Program to illustrate String Interpolation
// using StringBuilder class
import java.io.*;
class GFG {
public static void main(String[] args) {
String a = "Geeks for Geeks";
String b = "coding";
// Print an display the interpolated string
// using the StringBuilder class and append() method
System.out.println(
new StringBuilder(a)
.append(" is the best platform to learn ")
.append(b)
.append("."));
}
}
OutputGeeks for Geeks is the best platform to learn coding.
When to Use Which Method
format()
: Use this method
for simple and readable string interpolation with placeholders. This is ideal for small expressions.MessageFormat
class: Use this class, when you need indexed placeholders and want to avoid repeating the same variable multiple times.StringBuilder class
: Use this class
for complex or performance-sensitive cases where multiple string concatenations are involved.
Similar Reads
Convert String to Float in Java To convert a String to a Float in Java, there are several ways including built-in methods and handling potential exceptions for invalid inputs.Example: For this conversion, we will use the most common and efficient method i.e. parseFloat() method. This method converts a string into a primitive float
2 min read
Java String Exercise String in Java are the objects which can store characters of values in Java, it act the same as an array of characters in Java. Java String is one of the most important topics in Java programming. It is widely used to manipulate and process textual data. In this article, we will learn about Java Str
5 min read
Convert a String to a ByteBuffer in Java In Java, ByteBuffer can be used to perform operations at the Byte level one more thing is this class provides different types of methods for reading writing, and manipulating bytes in a structured way only. In this article, we will learn about String to ByteBuffer in Java. Java Program to Convert St
4 min read
Reverse a String in Java In Java, reversing a string means reordering the string characters from the last to the first position. Reversing a string is a common task in many Java applications and can be achieved using different approaches. In this article, we will discuss multiple approaches to reverse a string in Java with
7 min read
How to Convert Char to String in Java? In this article, we will learn how to Convert Char to String in Java. If we have a char value like 'G' and we want to convert it into an equivalent String like "G" then we can do this by using any of the following three listed methods in Java. Using toString() method of Character classUsing valueOf(
5 min read
How to Pad a String in Java? Padding a String in Java involves adding extra characters, which may be spaces, zeros, or other given characters to the beginning or end of a string to meet a desired length. This is normally used in formatting outputs or aligning texts.Example:The String.format() method is a simple way to pad strin
3 min read