Appendix C. leftovers: The Top Ten Things: (We Didn’t Cover)

image

Even after all that, there’s still a little more.

There are just a few more things we think you need to know. We wouldn’t feel right about ignoring them, and we really wanted to give you a book you’d be able to lift without training at the local gym. Before you put down the book, read through these tidbits.

1. Packages and imports

As we said in Chapter 9, classes and functions in the Kotlin Standard Library are grouped into packages. What we didn’t say is that you can group your own code into packages.

Putting your code into packages is useful for two main reasons:

  • * It lets you organize your code.

    You can use packages to group your code into specific kinds of functionality, like data structures or database stuff.

  • * It prevents name conflicts.

    If you write a class named Duck, putting it into a package lets you differentiate it from any other Duck class that may have been added to your project.

How to add a package

You add a package to your Kotlin project by highlighting the src folder, and choosing File→New→Package. When prompted, enter the package name (for example, com.hfkotlin.mypackage), then click on OK.

image

Package declarations

When you add a Kotlin file to a package (by highlighting the package name and ...

Get Head First Kotlin now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.