Interpolation in Java 20

Find this useful? Support us: Star on GitHub 6
Category: String | Language: Java 20

Interpolation is the act of replacing placeholders within a string (usually denoted by curly braces) with actual variable values. In Java, you can interpolate strings using the String.format method or by using the printf method. Here are some examples:

1. Using String.format() method:

String name = "John";
int age = 25;
String message = String.format("My name is %s and I am %d years old.", name, age);
System.out.println(message);

Output: My name is John and I am 25 years old.

2. Using printf() method:

String name = "Sarah";
double height = 5.8;
System.out.printf("My name is %s and I am %.2f feet tall.", name, height);
// %.2f specifies that we want to format the double variable with 2 decimal places
// %s specifies that we want to format the string variable
// The order of the placeholders should match the order of the variables passed as arguments

Output: My name is Sarah and I am 5.80 feet tall.

3. Interpolating values within a URL:

String name = "Tom";
int age = 35;
String url = String.format("https://example.com/user?name=%s&age=%d", name, age);
System.out.println(url);

Output: https://example.com/user?name=Tom&age=35

In summary, interpolation in Java can be achieved using the String.format or printf methods. The placeholders within the string are denoted by curly braces, and the actual values for the placeholders are passed as arguments in the order they appear within the string.