Extensions on built-in types in Kotlin
📖 Scenario: You are working on a Kotlin project where you want to add new useful functions to existing types without changing their original code. This is common when you want to make your code cleaner and easier to read.
🎯 Goal: You will create an extension function on the String type to count vowels, then use it in a program to show how many vowels a word has.
📋 What You'll Learn
Create an extension function on the
String type called countVowelsThe
countVowels function returns the number of vowels in the stringUse the extension function on a sample string
Print the result
💡 Why This Matters
🌍 Real World
Extension functions let you add new abilities to existing types like strings or numbers. This makes your code cleaner and easier to read, like adding new tools to your toolbox without changing the tools themselves.
💼 Career
Many Kotlin projects use extension functions to improve code quality and maintainability. Knowing how to write and use them is important for Kotlin developers in app development and backend services.
Progress0 / 4 steps