Prepare Interview

Mock Exams

Make Homepage

Bookmark this page

Subscribe Email Address

Question: What is String.formatted() in Java 15?
Answer:

We could previously replace placeholders in a string as follows, for example:

String message =    String.format(        "User %,d with username %s logged in at %s.",        userId, username, ZonedDateTime.now());

Starting from Java 15, we can use an alternative syntax:

String message =    "User %,d with username %s logged in at %s."        .formatted(userId, username, ZonedDateTime.now());

It makes no difference which method you use. Both methods will eventually call the following code:

String message =    new Formatter()        .format(            "User %,d with username %s logged in at %s.",            userId, username, ZonedDateTime.now())        .toString();

So the choice is ultimately a matter of taste. I quickly made friends with the new spelling.

Is it helpful? Yes No

Most helpful rated by users:

©2025 WithoutBook