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

bookType Inference in Kotlin

Kotlin uses a feature called type inference to automatically determine the type of a variable when you declare it and assign a value. This means you do not always need to specify the type explicitly—the compiler can often figure it out from the value you provide. Type inference helps make code more concise and readable, but there are situations where you still need to declare the type explicitly.

Here is how type inference works in practice:

Main.kt

Main.kt

copy
123456789101112131415161718192021222324252627282930313233
package com.example fun main() { // Type inferred as Int val number = 10 // Type inferred as String val greeting = "Hello, Kotlin!" // Type inferred as Double val pi = 3.14159 // Explicit type declaration val age: Int = 25 // Explicit type declaration for clarity val name: String = "Alice" // Type inference with var (mutable variable) var city = "New York" city = "San Francisco" // Still a String // Uncommenting the following line would cause a type error: // city = 123 // Error: The integer 123 is not a String println("number: $number") println("greeting: $greeting") println("pi: $pi") println("age: $age") println("name: $name") println("city: $city") }

In the code above, you see both inferred and explicitly declared types. When you write val number = 10, Kotlin knows number must be an Int because 10 is an integer. The same logic applies to greeting, pi, and city. You only need to specify a type—such as val age: Int = 25—if you want to make the type clear, if the value could be of multiple possible types, or if you are declaring a variable without assigning a value immediately. Explicit type declarations are also useful for readability or when working with APIs that expect a specific type.

Type inference keeps your code simple, but always remember: if the compiler cannot determine the type, or if you want to prevent accidental mistakes, specify the type explicitly.

question mark

Which of the following statements about type inference in Kotlin are true?

Select all correct answers

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

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

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

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

Запитати АІ

expand

Запитати АІ

ChatGPT

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

bookType Inference in Kotlin

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

Kotlin uses a feature called type inference to automatically determine the type of a variable when you declare it and assign a value. This means you do not always need to specify the type explicitly—the compiler can often figure it out from the value you provide. Type inference helps make code more concise and readable, but there are situations where you still need to declare the type explicitly.

Here is how type inference works in practice:

Main.kt

Main.kt

copy
123456789101112131415161718192021222324252627282930313233
package com.example fun main() { // Type inferred as Int val number = 10 // Type inferred as String val greeting = "Hello, Kotlin!" // Type inferred as Double val pi = 3.14159 // Explicit type declaration val age: Int = 25 // Explicit type declaration for clarity val name: String = "Alice" // Type inference with var (mutable variable) var city = "New York" city = "San Francisco" // Still a String // Uncommenting the following line would cause a type error: // city = 123 // Error: The integer 123 is not a String println("number: $number") println("greeting: $greeting") println("pi: $pi") println("age: $age") println("name: $name") println("city: $city") }

In the code above, you see both inferred and explicitly declared types. When you write val number = 10, Kotlin knows number must be an Int because 10 is an integer. The same logic applies to greeting, pi, and city. You only need to specify a type—such as val age: Int = 25—if you want to make the type clear, if the value could be of multiple possible types, or if you are declaring a variable without assigning a value immediately. Explicit type declarations are also useful for readability or when working with APIs that expect a specific type.

Type inference keeps your code simple, but always remember: if the compiler cannot determine the type, or if you want to prevent accidental mistakes, specify the type explicitly.

question mark

Which of the following statements about type inference in Kotlin are true?

Select all correct answers

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

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

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

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