Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

The process of formatting strings in Java involves using the String.format() method or the System.out.printf() method. Both methods use format specifiers to denote where to insert values into the string.

Example of using String.format() method:

String name = "John";
int age = 30;
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 30 years old.

Example of using System.out.printf() method:

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

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