You’ll do less typing—that act of hammering keys on the keyboard—to create applications with Kotlin. That’s because a lot of things we’ve come to take for granted as required are optional in Kotlin.

Semicolons are optional

When you start programming in Kotlin, your right pinky immediately gets relief from repetitive stress injury that it may have endured for most of your programming career. Though you could, you don’t have to end every single statement or expression with a semicolon. Use the semicolon sparingly—only on occasions when you want to place two or more expressions or statements on a single line.

The following example, is valid syntax in Kotlin, and can be written standalone:

6 * 2

At first thought it may not appear to be a big deal, but, as you’ll see later in the book, not having to place semicolons makes the code fluent, especially when creating internal DSLs.

If you’re transitioning into Kotlin from languages like Java and JavaScript, chances are that hitting the semicolon key is an involuntary action by now. That’s understandable, but make it a habit to leave out semicolons when writing in Kotlin.

Variable type specification is optional

Kotlin is statically typed, but that doesn’t mean you have to specify the details of variables’ types. Static typing means that the type of variables is verified and type sanity is guaranteed at compile time.

Kotlin has the smarts—type inference—to determine the type of variables based on the context. Let’s define a variable without specifying the type and then ask for the type of that variable.

Create a free account to view this lesson.

By signing up, you agree to Educative's Terms of Service and Privacy Policy