In our last blog, we discuss what is Kotlin? and what are the Advantages of Kotlin?
So after getting this little bit knowledge about it now, we can move on basics.
Let’s start with the Variable declaration,
it uses two different keywords to declare variables: val and var.
Var keyword is known as a mutable variable. It means that the variable can be changed.
Example:
var count: Int = 10
count = 15
-> Val keyword is known as an immutable variable. It means that the variable cannot be changed.
Example:
val languageName: String = “Kotlin”
-> If you are trying to reassign immutable variable than its throw compiler error like
“val cannot be reassigned”.
Read More: 7 Biggest Advantages of Kotlin over Java
it is a statically typed language. So the type of variables is known during the compile time.
Example:
val language: Int
val marks = 12.3
Here, the compiler knows that language is Int and marks is Double type before compile time.
There are few types in Kotlin,
There are 6 built-in types same like JAVA,
To represent Character type in Kotlin, Char keyword is used.
In Java, Char types can be treated as Number but that things give an error in Kotlin.
In Java, you can do something like:
char letter = 65;
However, the following code gives an error in Kotlin.
var letter: Char = 65 // Error
The Boolean data type has two possible values, either true or false.
val flag = true
Arrays in Kotlin are not native objects but they are instances of Array class. Creating an array is slightly different in Kotlin. In Kotlin you have to call a constructor or a factory function that makes the array.
The arrayOf function takes a list of values all of the same types and returns them as an array.
Example:
var number = arrayOf(1,2,3)
In other languages, variable usually contains null value if you are not going to initialize. But variable cannot hold null value by default. So the following example is invalid.
// Fails to compile
val languageName: String = null
-> If you want to hold null value in a variable then you have to follow below example.
val languageName: String? = null
it’s features several mechanisms for implementing conditional logic. The most common of these is an if-else statement.
if (count == 42) {
println("I have the answer.")
} else {
println("The answer eludes me.")
}
-> If you want to print String from if condition then you have to see the following example.
val answerString: String = if (count == 42) {
"I have the answer."
} else if (count > 35) {
"The answer is close."
} else {
"The answer eludes me."
}
println(answerString)
When expression in Kotlin is replacement of switch statement from the other language.
Example:
var dayOfWeek = 4
when(dayOfWeek) {
1 -> println("Monday")
2 -> println("Tuesday")
3 -> println("Wednesday")
4 -> println("Thursday")
5 -> println("Friday")
6 -> println("Saturday")
7 -> println("Sunday")
else -> println("Invalid Day")
}
While loop executes a block of code repeatedly as long as a given condition is true.
while(condition) {
// code to be executed
}
A for-loop is used to iterate through ranges, arrays, collections, or anything that provides an iterator.
iterating through the range:
for(value in 1..10) {
print("$value ")
}
iterating through arrays:
var primeNumbers = intArrayOf(2, 3, 5, 7, 11)
for(number in primeNumbers) {
print("$number ")
}
So, this is all about what is kotlin’s and its advantages to create Your android app in kotlin. Now we talking about how to work on it so please keep connected with us for the next blog.
So, this is basic of Kotlin that how to declare a variable and how to use it, its data types, etc.
Wants to learn more about it? We are providing a more blog on this topic very soon. Please feel free to contact us for any of your queries.
Using dynamic packaging technology, one can encapsulate flights, accommodation, cars,…
In today’s contemporary era of accelerated digital evolution, the importance…
The fact is that in today’s internet environment the only…
This website uses cookies.