HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to KotlinVariables (val / var)Data TypesStringsListsConditions (if / when)Loops (for / while)Functions
REFERENCE
📖 Overview
🖥️ Output
📦 Variables & Types
🔢 Numbers & Math
✍️ String
📚 Collection
🔀 Condition & Loop
🧩 Function
Kotlin›Data Types

Data Types

Int, Double, Boolean, String; type conversion and Kotlin's null safety.

Simple enough for a 5-year-old to understand

Basic types

Every value has a type: integer (Int), decimal (Double), true/false (Boolean), text (String).

Example code
1
val number = 7
Int — whole number.
VariableInt — whole number.
2
val price = 3.14
Double — decimal, with a dot.
VariableDouble — decimal, with a dot.
3
val isOpen = true
Boolean — true/false.
VariableBoolean — true/false.
4
val text = "Kotlin"
String — text.
VariableString — text.
Variable
💡 Hint: Use a dot, not a comma, for decimals: 3.14 is correct, 3,14 is wrong.

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (1 left)

1.What is the type of the value 3.14?

About this topic

Kotlin's basic types are Int, Long, Double, Float, Boolean, Char, and String. Unlike Java, the distinction between primitive types and object types is not visible in the source code in Kotlin; the compiler chooses the appropriate one itself.

The most distinctive feature of the language is null safety. Every type has two forms: non-null (String) and nullable (String?). You cannot directly access a nullable value; a safe call, Elvis operator, or check is required. Thus, the null pointer exception is moved to compile time.

There is no implicit widening between types. While an Int automatically converts to Long in Java, Kotlin rejects this and requires explicit conversion. Although it may seem like extra typing, it prevents numerical errors from silently propagating.

What you will learn

  • Basic types and their use cases
  • Nullable types
  • Safe call and Elvis operator
  • Explicit type conversion
  • The behavior of integer division

Who is this for?

  • Learners of null safety
  • Those transitioning from Java and expecting automatic conversion
  • Those getting unexpected results in numerical calculations

When to use?

Every field coming from the server that might be missing should be modeled with a nullable type; this way, you won't forget to check it.

In monetary calculations, BigDecimal should be used instead of Double; floating-point rounding errors are unacceptable in financial results.

Common mistakes

Wrong

val a = 7
val b = 2
println(a / b)

When two Ints are divided, the result is still an Int and the decimal part is truncated; 3 is printed to the screen. This is not a bug but a rule of the language, yet it silently produces incorrect results in calculations. For a decimal result, at least one side must be a Double.

Correct

val a = 7
val b = 2
println(a.toDouble() / b)

Wrong

val name: String? = null
println(name.length)

You cannot directly access a nullable value with a dot; the compiler gives the error "Only safe (?.) or non-null asserted (!!.) calls are allowed". This is the compile-time equivalent of the null pointer error you would get at runtime in Java.

Correct

val name: String? = null
println(name?.length ?: 0)

Wrong

val value: String? = null
println(value!!.length)

The double exclamation mark means "this is definitely not null", and if the value is null, the program crashes with a NullPointerException. It disables all of Kotlin's null safety with a single mark. It should never be used anywhere you are not completely sure the value is not null.

Correct

val value: String? = null
println(value?.length ?: 0)

Frequently asked questions

What exactly does the safe call operator do?

If the value on the left is null, it does not execute the expression at all and returns null as the result; if it is not null, it calls it normally. It can be chained so that even if any step of a series of accesses returns null, the program does not crash.

What is the Elvis operator?

It is an operator consisting of a question mark and a colon; if the expression on the left is null, it returns the fallback value on the right. It is the shortest way to assign a default value, and an early return can also be written on the right side.

Why doesn't Int automatically become Long?

Kotlin removed implicit widening because it made it ambiguous which version of overloaded functions was being called. You write the conversion explicitly with methods like toLong; the intention becomes visible in the code.

Summary

Types are divided into nullable and non-nullable.

A nullable value is accessed with a safe call.

The Elvis operator provides a fallback value.

The double exclamation mark disables null safety.

Division of two Ints truncates the decimal part.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code