Kotlin is rapidly rising among the most popular programming languages. With Google now supporting Kotlin as the primary choice for Android app development over Java, more developers are switching. The demand for Kotlin developers in the job market has soared, reflecting this shift. Beyond mobile development, Kotlin’s versatility extends to web applications, embedded devices, and much more.
What is Kotlin
Kotlin is a modern programming language developed by JetBrains, known for its concise, expressive, and flexible syntax. Officially endorsed by Google as the preferred language for Android development in 2019, Kotlin has gained significant popularity for building Android apps, thanks to its compatibility with Java and ability to simplify complex code.
Kotlin and Java
If you are coming from Java, you will find that Kotlin looks similar but also has some big differences. Kotlin is powerful and has a lot of syntax shortcuts, which can feel a little confusing at first.
Kotlin is designed to be interoperable with Java, allowing you to use Kotlin code within your existing Java projects. This makes switching to Kotlin easier, letting you enjoy its features without needing to start everything from scratch.
Our Kotlin Roadmap
- Main function
In Kotlin, fun main() is the entry point for a Kotlin program. This is where the program starts executing. Similar to Java’s public static void main(String[] args), the main function in Kotlin is defined with the fun keyword (short for “function”) and can look like this:
- Variables
To declare a variable, use the var keyword.
To declare a constant, use the val keyword. val in Kotlin is the same as final in Java.
Initialize a variable to null using the “?” operator. If the “?” operator is not provided the compiler won’t allow you to assign null to the variable
If we need to concatenate a String with a number we use $
We can declare without specifying the type
- Variables Types
Int – Double – Float – String – Boolean
- Arithmetic operations
- String functions
You can check for more Kotlin String functions:
Common String Extension Functions in Kotlin | Baeldung on Kotlin
- Comparisons
Meaning: Checks if x is equal to y.
Returns true if both x and y are equal, otherwise false
Meaning: Checks if x is not equal to y.
Returns true if x and y are not equal, otherwise false
Meaning: This is the negation of the equality check.
It checks if x is not equal to y using logical negation.
Equivalent to x != y
Meaning: Checks if x is greater than or equal to y.
Returns true if x is either greater than or equal to y, otherwise false.
Meaning: Checks if x is less than or equal to y.
Returns true if x is either less than or equal to y, otherwise false
Meaning: Checks if x is greater than y.
Returns true if x is greater than y, otherwise false.
Meaning: Checks if x is less than y.
Returns true if x is less than y, otherwise false.
Example
AND logic &&: is true if the two comparisons are true
OR logic ||: is true if at least a comparison is true
&& is stronger than ||
- If condition
or
- User input
Readln: prompt the user to enter something and return it as a string
toInt: function will convert the entered number (returned by readln() as string) to an int, but if the user enters a character, the program crashes.
toIntOrNull: function will convert the entered number (returned by readln() as string) to an int but if the user entered a character it would return a null and the program won’t crash.
- Array
- While loop
Example
- For condition
- Lists
The difference between an array and a list is
We can println(list), but arrays cannot
Data cannot be changed in lists as arrays -> list[0]=9 false
We should declare mutable list:
to declare a list
- when (as switch loop)
- Function and parameters
- Function with return values
List as parameter in functions
- vararg parameters
- Default and named parameters
- Extension function
- Classes
We create classes where we can put variables in constructors and functions and we call them in the main file by creating an instance
then we create an instance in the main file
- Heritage
Create a superclass with a constructor and its variables and functions
In subclass
In the main file create an instance of subclass where you can call superClass’s variables and functions
ps: in the subclass, we inherit variables in order to change their values
in the main fun we create an instance of subclass in order to inherit the superclass’s functions with variables
- Visibility modifiers
in the main file we create an instance of subclass in order to inherit the superclass’s functions with variables -> to avoid manipulation with variables we use visibility modifiers
Private: the variables can be accessed just by the current class
Protected: the variables can be accessed just by the super and subclass and not the main file
public: variables accessed everywhere
- Abstract classes
It is a class that has undefined abstract functions and defined classes
Abstract parent class has:
Normal variables
Functions are abstracts
In the subclasses we override and define the functions
In the main class, functions are called by objects (instances)
You can’t create an instance for the parent class because it’s abstract
- Objects and companion objects
You create a file object and set the necessaire variables and functions, we don’t need to create an instance in the main fun we call directly what we need from the object file
The companion object is a special construct that allows you to define properties and methods on a class that can be accessed directly without an instance of the class. It provides a way to define static members within a class.
- Anonymous classes
anonymous classes allow you to create a class without explicitly declaring its name. It is a way to define a class and create an instance of it at the same time. Anonymous classes are often used when you need to provide an implementation of an interface or extend a class without creating a named subclass.
So, we can’t create an instance for an abstract class or interface the syntax is:
- Exceptions
custom exception
- Lmbda functions
a lambda function is an anonymous function that can be used as an expression. It is defined within curly braces {} and can have parameters followed by an arrow -> and an expression.
Here is the general syntax for a lambda function in Kotlin:
{ parameterList -> body }
Examples:
- Simple Lambda:
- Using Lambda with Higher-Order Functions:
map:
filter:
- Generics
Generics in Kotlin allow you to define classes, interfaces, and functions with type parameters, which provide a way to reuse code with different data types while ensuring type safety. By using generics, you can create a single implementation that works with any type, rather than having to duplicate the implementation for each specific type.
1- Defining Generic Classes
You can define a generic class by placing the type parameter in angle brackets `<T>` after the class name:
Example:
2-Generic Functions
Functions can also be generic, with type parameters defined before the function name:
Example:
3-Multiple Type Parameters
You can define multiple type parameters by separating them with commas:
Example:
Generics are a powerful feature that can make your code more flexible, reusable, and type-safe by allowing you to work with different data types while ensuring type consistency.
What to learn next
The journey after learning the basics of Kotlin involves mastering more advanced features and tools, which will allow you to write elegant, efficient, and scalable applications. You should focus on exploring the areas that align with your interests—whether that’s Android development, backend services, or functional programming—and continuously build projects to solidify your knowledge. The Kotlin ecosystem is rich and growing, so there’s always something new to learn!