7

Groovy supports string interpolation operations. Here is an example

def name = "Tom"
println("Hello my name is ${name}")

Does Java 8 support string interpolation as the way Groovy does?

Any answer, greatly appreciated. Thanks

Dan Smith
  • 423
  • 2
  • 5
  • 17

2 Answers2

1

Java8 has nothing to do with this. For formatted console output, you can use printf() or the format() method of System.out Try this out.

System.out.printf("My name is: %s%n", "Tom");
Ravindra Ranwala
  • 20,744
  • 6
  • 45
  • 63
0

For formatted console output use of String.format is recommanded. System.out.println(String.format("My name is: %s%n", "Tom"));

When logging a message there are several important requirements which must be fulfilled:

  • The user must be able to easily retrieve the logs The format of all the logged message must be uniform to allow the user to easily read the log.
  • Logged data must actually be recorded.
  • Sensitive data must only be logged securely.
  • If a program directly writes to the standard outputs, there is absolutely no way to comply with those requirements.

That's why defining and using a dedicated logger is highly recommended. org.slf4j.Logger is one of the standard Logger factory and is easy to use.

private static final Logger LOGGER = LoggerFactory.getLogger(<ClassName>.class);
LOGGER.info("My name is: {}", "Tom");
Dharita Chokshi
  • 1,133
  • 3
  • 16
  • 39