Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Variables and Data Types  ›  Lesson

Characters, Strings and Booleans

Strings 12 minA Char holds one character in single quotes, a String holds immutable text in double quotes and compares by content with ==, and a Boolean holds true or false combined with &&, || and !.

Characters, Strings and Booleans

A loyalty code such as "P7", the letter on a cup, the question "is the café open?" — much of a program is text and yes/no answers rather than arithmetic. Kotlin has three types for these, and each has one detail worth knowing.

The rule

'c' is one Char, "text" is a String, true/false is a Boolean.

Char — a single character in single quotes.

val size: Char = 'M'
println('7'.digitToInt())   // 7   — the digit's value
println('7'.code)           // 55  — its Unicode number

A Char is not a number in Kotlin: val n: Int = 'A' is an error. Special characters use escapes: '\n' (new line), '\t' (tab), '\'' (a single quote).

String — text in double quotes. Strings are immutable: every "change" makes a new string.

val code = "P7"
println(code.length)        // 2
println(code[0])            // P   — indexing gives a Char
println(code.lowercase())   // p7  — a new string; code is unchanged
println(code == "P7")       // true — == compares contents

For text over several lines, a raw string uses three quotes. It keeps line breaks as typed and does not process escapes. trimIndent() removes the common leading spaces:

val sign = """
    Pebble Café
    Open 8 to 6
""".trimIndent()

Boolean — only true or false. Combine with && (and), || (or) and ! (not). && and || stop early: in a && b, if a is false, b is never evaluated.

val isOpen = true
val hasBeans = false
println(isOpen && hasBeans)  // false
println(isOpen || hasBeans)  // true
println(!hasBeans)           // true

The shortcut

Look at the quotes: single → Char, double → String. Then remember that indexing a String gives a Char, so code[1] == '7' is right and code[1] == "7" compares a Char with a String and does not compile.

Is "P7" a valid code?Step 1 of 4
  1. Check the length

    code.length == 2 is true.

Drills

Answer fast — trust the rule.

quick drill

What is the type of "A"?

quick drill

val code = "P7". What is code[1]?

quick drill

What does '7'.digitToInt() return?

quick drill

val s = "latte"; s.uppercase(). What is s afterwards?

quick drill

In isOpen && checkStock(), when is checkStock() not called?

Remember

  • Single quotes make a Char, double quotes a String; indexing a String gives a Char.
  • Strings are immutable and == compares their contents.
  • && and || stop early once the answer is known.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainChecking a customer code

Loyalty codes at the café look like "P7". Explain how you would get the first character and turn the second one into the number 7, why comparing two codes with == is correct, and how you would test that the code has length 2 and starts with 'P'.