Skip to main content
PreparationZone Logo
PreparationZone
Back to Java

Java · Easy · Free

Text blocks (Java 15) — indentation, escaping, and JSON/SQL use cases?

JavaJava 17+Fresher
Model answer

Text blocks

Text blocks (""" ... """) embed multi-line strings with readable indentation. The compiler strips incidental indentation based on the closing delimiter position.

  • Escape sequences still work; trailing whitespace on lines can be trimmed with \.
  • Great for SQL, JSON fixtures, and OpenAPI snippets in tests.
  • .formatted() or String.format for interpolation.
  • Don't use for large prod SQL without parameterization—still inject via PreparedStatement.
java
String sql = """
        SELECT id, name
        FROM users
        WHERE tenant_id = ?
        """;
PreparedStatement ps = conn.prepareStatement(sql);
Interview: mention incidental vs essential whitespace rules—closing quote alignment matters.