Basics

Kotlin println

Printing with println

Kotlin println outputs to console with string templates.

Introduction to Kotlin println

The println function in Kotlin is used to print a message to the console. It is a part of the standard Kotlin library and is commonly used for debugging and displaying information to the user. This function automatically appends a newline character at the end of the message, making it convenient for outputting lines of text.

Basic Usage of println

Using println is straightforward. You can pass a string or any other datatype, and Kotlin will convert it to a string if necessary. Here's an example of basic usage:

String Templates in println

Kotlin's println function supports string templates, which allow you to embed variables and expressions directly into strings. You can use the {expression} syntax to evaluate expressions within the string. Here is an example:

Advanced Usage with Expressions

You can include complex expressions within string templates using curly braces. This is particularly useful for calculations or function calls within a println statement. Consider the following example:

Conclusion

The println function in Kotlin is a versatile tool for console output. By leveraging string templates, you can easily format and print dynamic content. Whether for simple messages or complex expressions, println provides a straightforward way to display information.

Previous
Gradle