Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте The Unit Type | Nullable and Special Types
Practice
Projects
Quizzes & Challenges
Вікторини
Challenges
/
Kotlin Data Types

bookThe Unit Type

Свайпніть щоб показати меню

In Kotlin, the Unit type is a special type that indicates a function does not return a meaningful value. This is similar to the void return type in languages like Java, but Unit is actually a real type with a single value, also named Unit. You use Unit in function signatures when you want to show that the function performs an action but does not return any data.

Main.kt

Main.kt

copy
123456789
package com.example fun printMessage(message: String): Unit { println("Message: $message") } fun main() { printMessage("Hello from a Unit function!") }

In the code above, the printMessage function is declared to return Unit. When you call printMessage, it prints the message to the console but does not return any value you can use. In Kotlin, if you omit the return type in a function that does not explicitly return a value, Unit is used implicitly. This makes function signatures clear and consistent, and allows you to treat Unit as a regular type if needed.

question mark

What does a Kotlin function return if its return type is Unit?

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 3. Розділ 4

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

Секція 3. Розділ 4
some-alt