But now I wanna know, in Java (or Kotlin), how do I treat a string like a line of code?
The JVM ecosystem has a ... strong cultural aversion to this type of data/code commingling, due to the dangers it brings.
The traditional way to have dynamic code generation and execution on the JVM is to use something like Javassist (https://github.com/jboss-javassist/javassist) or ByteBuddy (https://github.com/raphw/byte-buddy) to generate a type with whatever behaviour you want it to have, then call it.
If you really want to, you can use something like JShell or BeanShell.