📝 Comments and Code Formatting in Kotlin
Comments help make your Kotlin code more understandable and maintainable. Kotlin supports both single-line and multi-line comments, similar to Java. Proper formatting improves readability and consistency.
💬 Types of Comments
- Single-line comment: Starts with
//and continues to the end of the line. - Multi-line comment: Starts with
/*and ends with*/, can span multiple lines.
🧾 Example:
// This is a single-line comment
/*
This is a multi-line comment.
It can span multiple lines.
*/
fun main() {
println("Hello, Kotlin!") // Print statement
}
🎨 Code Formatting Best Practices
- Use consistent indentation (default is 4 spaces).
- Place opening braces on the same line (Kotlin style guide).
- Use blank lines to separate logical code blocks.
- Keep lines under 100 characters for better readability.
⚙️ Auto-Formatting Tools
- IntelliJ IDEA / Android Studio: Use Ctrl + Alt + L (Windows/Linux) or Cmd + Option + L (Mac) to auto-format code.
- ktlint: A Kotlin linter and formatter you can integrate into your project for consistent style checking.


