Operators & Strings

Kotlin is a modern, concise language with a familiar set of operators for arithmetic, comparison, and logic — plus first-class string templates that make building text effortless.

Learn Operators & Strings in our free Kotlin course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.

Part of the free Kotlin course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

By the end of this lesson you'll know how every common operator behaves (including the gotcha of integer division) and how to manipulate strings with templates and built-in functions.

What You'll Learn in This Lesson

1️⃣ Arithmetic, Comparison, and Logic

Kotlin's operators will look familiar from maths and other languages. The one trap to remember: when both sides of / are integers, the result is an integer — the fractional part is thrown away. Make one side a Double to get a decimal result.

Comparisons ( , == , != ) always produce a Boolean , and the logical operators && , || , and ! let you combine those booleans into bigger conditions.

2️⃣ Strings and Templates

Strings in Kotlin come with a rich toolbox. Templates ( $var and {'$ '} ) are the idiomatic way to build text, and methods like uppercase() , trim() , and replace() transform strings without fuss.

Triple-quoted strings keep line breaks exactly as written; .trimIndent() strips the shared indentation so your source stays tidy while the output is clean.

Your turn. Replace each TODO with the right operator, then run and compare.

Combine arithmetic, a comparison, and a string template (with uppercase() ) into one tidy receipt line.

📋 Quick Reference — Operators & Strings

Practice quiz

What does 7 / 3 evaluate to in Kotlin?

  • 2.33
  • 2
  • 3
  • 2.5

Answer: 2. When both operands are integers, Kotlin performs integer division and truncates to 2.

How do you make 7 / 3 produce a decimal result?

  • Use 7 // 3
  • Make at least one operand a Double, like 7.0 / 3
  • Use 7 % 3
  • It is impossible

Answer: Make at least one operand a Double, like 7.0 / 3. If one operand is a Double, the whole expression becomes a Double.

What does the % operator compute?

  • The percentage of a number
  • Integer division
  • The remainder after division
  • Exponentiation

Answer: The remainder after division. % returns the remainder, so 7 % 3 is 1.

What does 7 % 3 evaluate to?

  • 2
  • 0
  • 3
  • 1

Answer: 1. 3 goes into 7 twice with 1 left over, so the remainder is 1.

Which operators are the logical and, or, and not?

  • & | ~
  • && || !
  • and or not only
  • AND OR NOT

Answer: && || !. Kotlin uses && for and, || for or, and ! for not.

What type does a comparison like 5 > 3 produce?

  • Int
  • String
  • Boolean
  • Double

Answer: Boolean. Comparison operators always produce a Boolean.

Which is the idiomatic way to insert a value into a string?

  • "Hi " + name
  • String.format with %s
  • A string template like "Hi $name"
  • Concatenating with commas

Answer: A string template like "Hi $name". String templates with $ are clearer and the idiomatic choice.

How do you embed an expression (not just a variable) in a template?

  • $a + b
  • ${a + b}
  • #{a + b}
  • {a + b}

Answer: ${a + b}. Use ${ } around an expression; bare $ only works for a simple variable.

What do triple-quoted strings provide?

  • Automatic encryption
  • Raw, multi-line strings that preserve line breaks
  • Faster concatenation
  • Compile-time constants only

Answer: Raw, multi-line strings that preserve line breaks. Triple-quoted strings are raw multi-line strings; trimIndent() cleans up indentation.

In Kotlin, what does == compare for two strings?

  • Their memory references
  • Their contents, by calling equals
  • Their lengths only
  • Nothing, it is a syntax error

Answer: Their contents, by calling equals. Unlike Java, Kotlin's == compares values (calls equals), so string contents are compared.