Search⌘ K
AI Features

More Expressions, Fewer Statements

Explore how Kotlin emphasizes expressions rather than statements, unlike Java. Understand the benefits of expressions that return values and avoid side effects, enabling clearer, less error-prone code. This lesson covers Kotlin's use of if and try-catch as expressions and explains why assignments are treated differently, improving your grasp of concise Kotlin syntax.

Languages like Java, C#, and JavaScript have more statements than expressions—if statement, for statement, try, and so on. On the other hand, languages like Ruby, F#, Groovy, Haskell, and many others have more expressions than statements. Let’s discuss which is better before we discuss Kotlin’s preference.

Drawbacks of statements

While statements are prevalent, they have a dark side—they don’t return anything and have side effects. A side effect is a change of state: mutating a variable, writing to a file, updating to a database, sending data to a ...