AlgoGalaxy

AlgoGalaxy

University
CodeRush
Jobs
Tools
Insights
Explore
Personalize
JavaGoPHPRustGraphsTreesArraysDynamic ProgrammingGreedyHashingHeapLinked ListNumber TheoryStackStringsTrieUnion FindBacktrackingBinary SearchBit Manipulation

Kotlin Data Types

Kotlin data types determine which operations are valid and how values are stored, compared, and passed through a program. Start with the small built-in types—numbers, booleans, characters, and strings—then make nullability explicit when absence is valid. A type is part of the contract: it tells the compiler and the reader what a value can safely represent.

Work through the examples in order. Each one changes only one idea at a time, so you can connect the code you write to the output you see. Do not skip the changed-input step: that is where syntax becomes a dependable programming habit.

What You Will Build

This page is a compact code lab, not a one-paragraph definition. The five examples move from the smallest valid form to a boundary case or reusable function. Keep a local Kotlin compiler open while reading: type each example, make the suggested change, and observe the output before continuing.

In real software, the same discipline protects larger systems. Name inputs clearly, validate data at the boundary, avoid logging secrets, and make an error or alternate result visible to the caller. Kotlin's syntax helps, but the programmer still owns the business rule.

Example 1: Integer values

Int represents common whole-number work such as counts, indexes, and small amounts. Use arithmetic on numeric values, not on text that only looks like a number, so invalid input is rejected at the boundary.

Kotlin

fun main() {
    val retries: Int = 3
    println(retries + 1)
}

Expected Result

4

Try next: Try an Int value at the edge of your intended range. Use Long when the domain can exceed Int rather than waiting for overflow.

Read the example in order: identify the input, trace the Kotlin expression or branch, then compare the printed result with the stated rule. A small change should produce a predictable new result; if it does not, make the contract explicit before adding more syntax.

Example 2: Decimal values

Double represents floating-point values and is useful for measurements and approximate calculations. Do not use binary floating-point for currency totals that require exact decimal arithmetic; choose an integer minor unit or a decimal type at that boundary.

Kotlin

fun main() {
    val temperature: Double = 21.5
    println(temperature + 1.0)
}

Expected Result

22.5

Try next: Use a decimal measurement, then write down whether the real domain accepts approximation or needs an exact representation.

Read the example in order: identify the input, trace the Kotlin expression or branch, then compare the printed result with the stated rule. A small change should produce a predictable new result; if it does not, make the contract explicit before adding more syntax.

Example 3: Boolean decisions

Boolean should name a concrete yes-or-no fact. A clear predicate makes conditions read as a policy statement instead of forcing callers to compare vague flags or magic numbers.

Kotlin

fun main() {
    val isPublished: Boolean = true
    println(isPublished)
}

Expected Result

true

Try next: Rename a vague flag such as enabled to state exactly what is enabled and for whom.

Read the example in order: identify the input, trace the Kotlin expression or branch, then compare the printed result with the stated rule. A small change should produce a predictable new result; if it does not, make the contract explicit before adding more syntax.

Example 4: Characters and strings

Char holds one Unicode character while String holds text. Keep text as text until a boundary needs a parsed number, date, or structured identifier; conversion is a rule that can fail and should be handled intentionally.

Kotlin

fun main() {
    val initial: Char = 'K'
    val language: String = "Kotlin"
    println(initial.toString() + language.drop(1))
}

Expected Result

Kotlin

Try next: Replace the initial with another letter. Notice that Char uses single quotes and String uses double quotes.

Read the example in order: identify the input, trace the Kotlin expression or branch, then compare the printed result with the stated rule. A small change should produce a predictable new result; if it does not, make the contract explicit before adding more syntax.

Example 5: Nullable values

String and String? are different contracts. A nullable value must be checked, transformed safely, or given a documented fallback before it is used where a non-null String is required.

Kotlin

fun main() {
    val nickname: String? = null
    val label = nickname ?: "Guest"
    println(label)
}

Expected Result

Guest

Try next: Replace null with a name, then decide whether Guest is a correct fallback or whether the caller should see a validation error.

Read the example in order: identify the input, trace the Kotlin expression or branch, then compare the printed result with the stated rule. A small change should produce a predictable new result; if it does not, make the contract explicit before adding more syntax.

Example 6: Make the input visible

Start by naming the value the next line depends on. This keeps the lesson input explicit before we apply a transformation or call an external boundary. We are applying that habit while revisiting Kotlin Data Types.

Keep the example small enough to trace. First predict the normal result; then change one input and explain the boundary result in your own words.

Kotlin

fun review(value: String?): String =
    value?.trim()?.takeIf { it.isNotEmpty() }?.uppercase()
        ?: "needs a value"

fun main() {
    println(review("Kotlin Data Types"))
}

Expected Result

KOTLIN DATA TYPES

Try next: Replace the sample value with one from your own notes, predict the result, and then run it.

Example 7: Guard an empty boundary

A small guard makes the allowed and rejected path visible. We want a caller to receive an intentional result instead of discovering an unexplained failure later. We are applying that habit while revisiting Kotlin Data Types.

Keep the example small enough to trace. First predict the normal result; then change one input and explain the boundary result in your own words.

Kotlin

fun review(value: String?): String =
    value?.trim()?.takeIf { it.isNotEmpty() }?.uppercase()
        ?: "needs a value"

fun main() {
    println(review("Kotlin Data Types"))
}

Expected Result

KOTLIN DATA TYPES

Try next: Try an empty value as well as the normal value and describe why the two outcomes differ.

Example 8: Keep the transformation pure

This step returns a value instead of mixing the rule with display or storage work. That separation gives us a small unit we can run, test, and reuse. We are applying that habit while revisiting Kotlin Data Types.

Keep the example small enough to trace. First predict the normal result; then change one input and explain the boundary result in your own words.

Kotlin

fun review(value: String?): String =
    value?.trim()?.takeIf { it.isNotEmpty() }?.uppercase()
        ?: "needs a value"

fun main() {
    println(review("Kotlin Data Types"))
}

Expected Result

KOTLIN DATA TYPES

Try next: Call the same rule twice with different inputs and confirm that each result depends only on its input.

Example 9: Make a branch deliberate

Use a named branch where the business rule changes. A future reader should be able to point at the condition and state the result for both sides. We are applying that habit while revisiting Kotlin Data Types.

Keep the example small enough to trace. First predict the normal result; then change one input and explain the boundary result in your own words.

Kotlin

fun review(value: String?): String =
    value?.trim()?.takeIf { it.isNotEmpty() }?.uppercase()
        ?: "needs a value"

fun main() {
    println(review("Kotlin Data Types"))
}

Expected Result

KOTLIN DATA TYPES

Try next: Change the boundary value by one and explain exactly which branch becomes active.

Example 10: Use a collection without hidden mutation

Collections become safer when we can see which value is read, which value is derived, and whether the original collection remains owned by the caller. We are applying that habit while revisiting Kotlin Data Types.

Keep the example small enough to trace. First predict the normal result; then change one input and explain the boundary result in your own words.

Kotlin

fun review(value: String?): String =
    value?.trim()?.takeIf { it.isNotEmpty() }?.uppercase()
        ?: "needs a value"

fun main() {
    println(review("Kotlin Data Types"))
}

Expected Result

KOTLIN DATA TYPES

Try next: Add one more item and predict the derived result before executing the code.

Example 11: Return a named outcome

A named outcome is easier to test than an implied side effect. We can now distinguish a successful path from a rejected path without guessing from console noise. We are applying that habit while revisiting Kotlin Data Types.

Keep the example small enough to trace. First predict the normal result; then change one input and explain the boundary result in your own words.

Kotlin

fun review(value: String?): String =
    value?.trim()?.takeIf { it.isNotEmpty() }?.uppercase()
        ?: "needs a value"

fun main() {
    println(review("Kotlin Data Types"))
}

Expected Result

KOTLIN DATA TYPES

Try next: Rename the outcome in your own words while preserving the same success and failure rule.

Example 12: Trace a small loop

Work through one value at a time. The point is not to memorize syntax; it is to see how the accumulator or output changes after each repeatable step. We are applying that habit while revisiting Kotlin Data Types.

Keep the example small enough to trace. First predict the normal result; then change one input and explain the boundary result in your own words.

Kotlin

fun review(value: String?): String =
    value?.trim()?.takeIf { it.isNotEmpty() }?.uppercase()
        ?: "needs a value"

fun main() {
    println(review("Kotlin Data Types"))
}

Expected Result

KOTLIN DATA TYPES

Try next: Use a shorter collection first, then add one value and trace the extra iteration aloud.

Example 13: Keep display at the edge

The core rule prepares a value, while the outer line decides how to show it. That keeps the result usable by a UI, test, API, or command-line caller. We are applying that habit while revisiting Kotlin Data Types.

Keep the example small enough to trace. First predict the normal result; then change one input and explain the boundary result in your own words.

Kotlin

fun review(value: String?): String =
    value?.trim()?.takeIf { it.isNotEmpty() }?.uppercase()
        ?: "needs a value"

fun main() {
    println(review("Kotlin Data Types"))
}

Expected Result

KOTLIN DATA TYPES

Try next: Change only the display text and verify that the underlying rule and result stay unchanged.

Example 14: Test the closest boundary

A useful example includes the nearest boundary, not only the happy path. We are making the behavior inspectable before a real caller relies on it. We are applying that habit while revisiting Kotlin Data Types.

Keep the example small enough to trace. First predict the normal result; then change one input and explain the boundary result in your own words.

Kotlin

fun review(value: String?): String =
    value?.trim()?.takeIf { it.isNotEmpty() }?.uppercase()
        ?: "needs a value"

fun main() {
    println(review("Kotlin Data Types"))
}

Expected Result

KOTLIN DATA TYPES

Try next: Try the lowest supported value and then one value just outside the accepted range.

Example 15: Write a tiny regression check

A compact assertion turns the lesson into a repeatable promise. It helps us catch a changed rule before a larger program hides the source of the mistake. We are applying that habit while revisiting Kotlin Data Types.

Keep the example small enough to trace. First predict the normal result; then change one input and explain the boundary result in your own words.

Kotlin

fun review(value: String?): String =
    value?.trim()?.takeIf { it.isNotEmpty() }?.uppercase()
        ?: "needs a value"

fun main() {
    println(review("Kotlin Data Types"))
}

Expected Result

KOTLIN DATA TYPES

Try next: Deliberately change the expected value once, observe the failed check, and then restore the correct contract.

Practice Checklist

A tutorial becomes useful only when you can produce the behavior without copying the last code block. Complete the five short repetitions below, then write one new version that uses your own names and values.

  • Practice 1: reproduce “Integer values” from memory, change one input, and explain the result before running it.
  • Practice 2: reproduce “Decimal values” from memory, change one input, and explain the result before running it.
  • Practice 3: reproduce “Boolean decisions” from memory, change one input, and explain the result before running it.
  • Practice 4: reproduce “Characters and strings” from memory, change one input, and explain the result before running it.
  • Practice 5: reproduce “Nullable values” from memory, change one input, and explain the result before running it.

For a stronger check, explain one invalid input and the response the caller should receive. A generic crash, an unlabelled default, or a printed error is not a complete failure contract.

Common Mistakes

  • Copying a code block without changing an input, then assuming the rule is understood.
  • Using a convenient fallback where the caller actually needs a validation error, retry state, or explicit missing result.
  • Hiding mutable state, external work, or an exception behind a short expression that a future reader cannot safely review.
  • Treating type inference as a reason to hide an important numeric range, nullable value, or public API contract.
logo of AlgoGalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

AlgoGalaxy University

University HomeTechnology LearningCoding ProblemsLearning ProgressCareer OpportunitiesTechnology InsightsUtilNexa ToolsAlgoGalaxy HomeContact Support

AlgoGalaxy CodeRush

CodeRush HomeProblem SetsProgress TrackerLearn TechnologyTechnology JobsEngineering InsightsDeveloper UtilitiesAlgoGalaxy HomePrivacy Policy
Privacy Policy

 • 

Terms of Service

Copyright © AlgoGalaxy 2026