Introduction
- This series is going to be dedicated to understanding the Kotlin programming language. The first 3 posts will be a light introduction to the language and it's features and posts after those will be deep dives into certain aspects of the language.
GitHub
- The GitHub for this project can be found HERE
What is Kotlin?
- Kotlin is an open source statically typed(we must declare our types) language. In 2019 at the Google I/O event google announced that Android development will be increasingly
Kotlin first
. Which basically means that if you want to create Android projects you should learn Kotlin. So let's learn some Kotlin
Getting started
- Before we create our beloved
Hello world
program we need an environment for us to program in. To create this environment we are going to useGradle
. If you have never used or heard about Gradle before, don't worry, it's really just a fancy tool that keeps our code organized. - So before moving forward make sure you have Gradle installed on your machine.
- Now in order to create a Kotlin project, you should follow the official Gradle tutorial, which can be found HERE and then open that project in your IDE of choice.
Hello World
- So, thanks to Gradle, you and I both have a matching folder structure. Now find your way into the
App.kt
file and delete current main function and replace it with this:
fun main() {
println("Hello, world!")
}
In order to run this function all we have to do run the
gradle run
command in the command line(make sure you are in the same folder as our Gradle project). The output should beHello, world!
.Now lets work on breaking down what all this strange syntax means.
The
fun
keyword is used to declare a function. Which is why Kotlin nerds(us) say programming in Kotlin is fun. Functions also do not have to belong to a class like they do in Java. Theprintln("Hello, world!")
statement is used instead of the classicSystem.out.println
statement. Which is why Kotlin is said to be a veryexpressive
language. We only have to type a little to do a lot. Also, notice that we don't have to end the line with a semi colon.
Functions
- As you can see from the our hello world function, we can declare a function that has no return type by just not stating the return type. Which of course raises the question, how do we declare a return type? We can do so like this:
fun max(a: Int,b:Int): Int{
return if(a>b) a else b
}
- very similar to our hello world function. However, notice the that we have declared types on the variables
a: Int,b:Int
and for the return value of the function: Int
. In Kotlin, function return types are placed after the parameter list.
Statement and Expressions
- Keen observers of the max function may of noticed the peculiar way we have written the if statement. Actually, it's not an if statement at all, in Kotlin
if
is considered anexpression
. The difference between a expression and a statement being that an expression returns a value that can be used as part of another expression. Where a statement does not have its own reusable value. In Javaif
is always a statement.
Simplifying the function
- Since our
max()
function consist of a single expression we can remove the curly brackets and the return statement, giving us:
fun max2(a:Int,b:Int) = if(a > b) a else b
- This type of function with no curly brackets is called an
expression body
. Functions with curly brackets are calledblock body
. - You may of also noticed that we have removed the return type. Which seems odd for a statically typed language. So if we don't declare the return type, how does Kotlin know what to return? The compiler analyzes the expression used as the body of the function and uses it's type as the function return type. This type of analysis is called
type inference
(more on this in future posts). - However, omitting the return type is allowed only for functions with an expression body(function with no curly brackets). For a function with a block body(has curly brackets) we MUST define a return type.
Variables
- In Java, we start a variable declaration with a type. This is not how things are done in Kotlin. Kotlin lets us start with a keyword and we may or may not put the type after the variable name, like so:
val another = "one"
val age = 63;
var secondAge: Int = 33
- Similar to the expression body function from before, if we don't specify the type, the compiler analyses the initializer expression and uses it's type as the variable type.
Var vs Val
val : short for value, a variable declared with val
can't be reassigned once it has be initialized with a value. Similar to Java's final
keyword.
var : short for variable, variables declared with var can be reassigned as many times as we want.
- In General we should try to declare as many values as we can with the
val
syntax. Doing so will lead to cleaner code with less side effects.
Conclusion
- Thank you for taking the time out of your day to read this blog post of mine. If you have any questions or concerns please comment below or reach out to me on Twitter.
Latest comments (0)