Open In App

String Interpolation in Java

Last Updated : 19 Nov, 2024
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

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);
    }
}

Output
Geeks for Geeks is the best platform to learn coding

Other Ways to Perform String Interpolation

Some other ways to perform string interpolation in Java are as follows:

Using format() function

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));
    }
}

Output
Geeks for Geeks is the best platform to learn coding


Using MessageFormat Class

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));
    }
}

Output
Democracy 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("."));
    }
}

Output
Geeks 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