17

I wonder, is there any difference between these two:

val a = 123
println(f"hello1 $a") // 1                         
println(s"hello1 $a") // 2
Andrii Abramov
  • 10,019
  • 9
  • 74
  • 96
Incerteza
  • 32,326
  • 47
  • 154
  • 261

2 Answers2

20

According to the docs, f interpolation is typesafe. Also, it allows to add formatting right after the parameter, which s interpolation doesn't support.

Andrii Abramov
  • 10,019
  • 9
  • 74
  • 96
Ashalynd
  • 12,363
  • 2
  • 34
  • 37
10

The s interpolator allows you to plug in variables or expressions, while the f interpolator allows you to use formatting commands, à la C printf.

Note that contrary to C printf or Java String.format, the f interpolator will typecheck the arguments for you. This link has more information.

Philippe
  • 9,582
  • 4
  • 39
  • 59