Java String Interpolation

Java String Interpolation | The string interpolation is the way to concatenate two strings. This is the easier way without using more syntax. By using string interpolation the code becomes readable, compact, and efficient.

What is String Interpolation?
Strings that contain references to variables or expressions are called interpolated strings. When you use interpolated strings, the embedded expressions and variables are evaluated and the result is inserted into the string. The act of inserting data into a string is called string interpolation.

There are many ways to interpolate strings:-
1. By using string buffer and string builder class
2. By using MessageFormat class
3. By using format() method

String Interpolation Java using Concatenation

In this program, we will concate the student details using the concatenation operator.

public class Main {
   public static void main(String args[]) {
      String name = "Harry";
      String school = "St. Jhon's Convent";
      String address = "100 Church Street";
      String city = "Chicago";
      String pinCode = "60629";
      System.out.println("Student Details:");
      System.out.println("Student Name: " + name + 
            ", School: " + school + ", Address: " + 
            address + ", City: " + city
            + ", PinCode: " + pinCode);
   }
}

Output:-

Student Details:
Student Name: Harry, School: St. Jhon’s Convent, Address: 100 Church Street, City: Chicago, PinCode: 60629

Let us see it through the Student object. There we will override the toString() method to display the string object.

Java String Interpolation Example using Object

public class Student {

   private String name;

   private String school;

   private String address;

   private String city;

   private String pinCode;

   public void setName(String name) {
      this.name = name;
   }

   public void setSchool(String school) {
      this.school = school;
   }

   public void setAddress(String address) {
      this.address = address;
   }

   public void setCity(String city) {
      this.city = city;
   }

   public void setPinCode(String pinCode) {
      this.pinCode = pinCode;
   }
   
   // add getters if needed
   
   @Override
   public String toString() {
      return "Student [name=" + name + 
             ", school=" + school + 
             ", address=" + address +
             ", city=" + city + 
             ", pinCode=" + pinCode + "]";
   }  
}
public class Main {
   public static void main(String args[]) {
      Student student = new Student();
      student.setName("Harry");
      student.setSchool("St. Jhon's Convent");
      student.setCity("Chicago");
      student.setAddress("100 Church Street");
      student.setPinCode("60629");
      System.out.println("Student Details: \n" + student);
   }
}

Output:-

Student Details:
Student [name=Harry, school=St. Jhon’s Convent, address=100 Church Street, city=Chicago, pinCode=60629]

Java Interpolated Strings using format()

By using the string format() class we will interpolate strings. The method details of the format() method are as follows:-
public static String format(String text, Object Parameter)
For the same student example, we will use format().

Java String Interpolation Using format()

public class Main {
   public static void main(String args[]) {
      String name = "Harry";
      String school = "St. Jhon's Convent";
      String address = "100 Church Street";
      String city = "Chicago";
      String pinCode = "60629";
      System.out.println("Student Details: ");
      System.out.println(String.format("Student Name: %s, "
            + "School: %s, Address: %s, "
            + "City: %s, PinCode: %s", name,
            school, address, city, pinCode));
   }
}

Output:

Student Details:
Student Name: Harry, School: St. Jhon’s Convent, Address: 100 Church Street, City: Chicago, PinCode: 60629

Interpolated String Java using MessageFormat

Here, the interpolation is done by using MessageFormat class. The MessageFormat class is present in java.text package and it is just another implementation of the format method the difference is in the arrangement of placeholder. In the program {0} {1} {2} {3} {4} indicate the placeholder to be replaced with variable name in a sequential way.

Java String Interpolation using MessageFormat

import java.text.MessageFormat;

public class Main {
   public static void main(String args[]) {
      String name = "Harry";
      String school = "St. Jhon's Convent";
      String address = "100 Church Street";
      String city = "Chicago";
      String pinCode = "60629";
      System.out.println("Student Details: ");
      System.out.println(MessageFormat.format("Student Name: {0},"
            + " School: {1}, Address: {2},City: {3}, PinCode: {4}",
               name, school, address, city, pinCode));
   }
}

Output:-

Student Details:
Student Name: Harry, School: St. Jhon’s Convent, Address: 100 Church Street,City: Chicago, PinCode: 60629

Java String Interpolation using append()

Now we will use the append() method present in StringBuffer and StringBuilder class.

public class Main {
   public static void main(String args[]) {
      String name = "Harry";
      String school = "St. John's Convent School";

      System.out.println(new StringBuilder(name)
            .append(" is a Student of ").append(school));
      System.out.println(new StringBuffer(name)
            .append(" studies in ").append(school));
   }
}

Output:-

Harry is a Student of St. John’s Convent School
Harry studies in St. John’s Convent School

Also see:- Golden Ratio Java Program

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *