In Java, you can achieve similar functionality to Python’s f-strings using the String.format() method. Here’s an example:

Code:

String name = "Alice";
int age = 30;
double height = 1.75;
String message = String.format("My name is %s, I'm %d years old, and I'm %.2f meters tall.", name, age, height);
System.out.println(message);


In this example, %s is a placeholder for a string, %d for an integer, and %.2f for a floating-point number with 2 decimal places1. The variables name, age, and height are inserted in the order they are listed.

You can also use positional arguments in String.format(). For example:

Code:

String str1 = "GFG";
String str2 = "GeeksforGeeks";
String str = String.format("My Company name is: %1$s, %1$s and %2$s", str1, str2);
System.out.println(str);


In this example, %1$s refers to the first argument str1, and %2$s refers to the second argument str22. This allows you to repeat arguments or use them in a different order3. Happy coding!