dir.by  
  Search  
Programming, development, testing
Kotlin
What is an enumeration (enum) in Kotlin ?
  Looked at 1098 times    
 What is an enumeration (enum) in Kotlin ? 
last updated: 2 May 2024
Enumerations are a set of constants.
Declared with the word enum.
Example 1. values as Int
enum class BookType (val myValue: Int)
{
     Unknown (10),
     Fantasy (15),
     History (27),
     Child (28),
}
Full example:
  Kotlin  
enum class BookType (val myValue: Int)
{
     Unknown (10),
     Fantasy (15),
     History (27),
     Child (28),
}

fun main() {

     val myType1 = BookType.History

     println(myType1.myValue) // On the screen we will see 27
     println(myType1.name) // On the screen we will see History
}
 
 
Example 2. values as String
enum class BookType (val myValue: String)
{
     Unknown ("I don't know"),
     Fantasy ("It is very good to read fantasy books"),
     History ("I like read history books"),
     Child ("Lets see what child books I have"),
}
Full example:
  Kotlin  
enum class BookType (val myValue: String)
{
     Unknown ("I don't know"),
     Fantasy ("It is very good to read fantasy books"),
     History ("I like read history books"),
     Child ("Lets see what child books I have"),
}

fun main() {

     val myType1 = BookType.History

     println(myType1.myValue) // On the screen we will see I like read history books
     println(myType1.name) // On the screen we will see History
}
Writing a program
Step 1. Creating a new project
Step 2. Let's change the code in the file Main.kt
  Kotlin  
enum class BookType (val myValue: Int)
{
     Unknown (10),
     Fantasy (15),
     History (27),
     Child (28),
}

fun main() {

     val myType1 = BookType.History

     println(myType1.myValue)
     println(myType1.name)
}
Step 3. Launching the project
See the result at the bottom of the screen
 
← Previous topic
What is the difference between String and StringBuilder ?
 
Next topic →
How do I find enum by value in Kotlin ?
 
Your feedback ... Comments ...
   
Your Name
Your comment (www links can only be added by a logged-in user)

  Объявления  
  Объявления  
 
What is Kotlin?
Why is IntelliJ IDEA the most popular development environment for Kotlin?
Download and install IntelliJ IDEA to explore Kotlin
Create a new project in IntelliJ IDEA for study Kotlin
Int, Float, Boolean, Char ...
Integer numbers in Kotlin are: Byte, UByte, Short, UShort, Int, UInt, Long, ULong
Decimal numbers in Kotlin are: Float, Double
A flag with values of true or false in Kotlin is: Boolean
The symbol in Kotlin is: Char
Convert number to text in Kotlin | Int → String
String
The string, the text in Kotlin is: String
Interpolate strings in Kotlin. Example: val address:String = "${street}, ${country}"
What is the difference between String and StringBuilder ?
Enum
What is an enumeration (enum) in Kotlin ?
How do I find enum by value in Kotlin ?
Class
What is class in Kotlin? Example: class MyBook { ... }
lateinit is a late initialization for the class field | Kotlin
class that inherits from interface in Kotlin | Example: class MyBook : IBook { ...}
Unnamed class that inherits from interface in Kotlin | Example: val book1 = object : IBook { ...}
Generic, template class in Kotlin | Example: class MyBook<T> { ... }
Collections and Arrays
Create collections list, set, map and an array array in Kotlin
null
null value, use the symbol ? and ?. and !! and ?: and !!. and ?. in Kotlin
Lambda function
Lambda function in Kotlin. Example 1: var myFunc1 : (a:Int, b:Int) -> Int = { p1, p2 -> p1 + p2 };

  Ваши вопросы присылайте по почте: info@dir.by  
Яндекс.Метрика