Essential Guide — If you are moving from Java to Kotlin #1/2
Some of the basic syntax of Kotlin, which are useful if you are switching from Java.
Kotlin is easier than JAVA or other languages. Although JAVA language is used by Android developers, Kotlin is gaining popularity in the Android Community. So, this article is used as an essential guide moving to Kotlin from Java language.
In this article, we will cover the following major topics of Kotlin:
- Variables
- If statement
- when expression
- for loop
- while and do-while loop
- Nullable values and null check
- function
- class
- constructor
- Inheritance
1. Variables
Creating a variable is very easy. There are two keywords for creating a variable val
and var
.
val — Assigned a value only once
var — Assigned a value multiple times
2. If statement
In Kotlin, if
statement is similar to JAVA. Check it out…
Note: There is no use of Ternary operator(like, condition ? value1 : value2) in Kotlin.
Instead of the ternary operator, Kotlin use following syntax:
In Kotlin, if
can return a value as well (we have seen in the above point). So, it can be used in the return statement of a function, too.
3. When
when
expression is used as instead of switch
case (in Java). when
expression can be used to return a value, similar like if
expression.
In Kotlin, when
expression has Super Power. Let’s see this:
Let’s take some value for input and check out the output:
1. “Hi”
— “Input is a String”
2. ‘M’
— “Input is between A to Z”
3. 6
— “Input is between 1 to 10”
4. For loop
There are multiple representations or use-cases of for
loop. I have covered primary use-cases here. Let’s see some of the most popular use-cases:
1. in
— Simplest for loop which iterates over every element of the list
2. ..
— Iterate over a range
3. withIndex()
— Iterate over the list with an index of the current item.
FYI: If you want to know all the possible syntax of for loop in kotlin, then you should look in this article:
5. While and do-while loop
As we know, while
and do-while
loops have similar use-cases like for
loop, to loop over something or some condition.
while
loop first check the condition then execute the statements/block.
do-while
loop first executes the block then check condition.
6. Nullable values and null check
This is a very important concept for Android Developers if you are switching from Java to Kotlin.
In Kotlin, a reference must be explicitly marked as nullable when null
value is possible. ?
is used to indicate the possible nullable reference or variable.
7. Function
In Kotlin, the syntax of function/method is a little bit different from Java language.
- Following example having function with two Int parameters and Int return type:
2. function with an expression body and inferred return type:
3. function returning no meaningful value:
4. In the above case, Unit
return type can be omitted, too:
8. Class
In Kotlin, classes are declared using the class
keyword.
The class declaration consists of the class name, the class header, and the class body, surrounded by curly braces. Both the header and the body are optional; if the class has no body, curly braces can be omitted.
9. Constructors
In Kotlin, constructors are declared using the constructor
keyword.
The primary constructor is part of the class header: it goes after the class name (and optional type parameters).
10. Inheritance
All classes in Kotlin have a common superclass Any
, that is the default superclass for a class with no supertypes declared.
By default, Kotlin classes are final
, they can’t be inherited. To make a class inheritable, mark it with the open
keyword. To declare an explicit supertype, place the type after a colon in the class header.
I hope, this article will helpful for you. Thanks for reading!
About the Author
Hi There, I am Meet K Parmar. I am a final year student, pursuing Computer Engineering. I write stories on Tech and Programming. I love contributing to open source projects.