Mobile ApplicationSolutions

Kotlin: Variables Declaration, Data Types, and Conditionals

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,

How to Create a Variable?

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”.

Kotlin Basic Types

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,

  • Numbers
  • Characters
  • Booleans
  • Arrays

1. Number Type

There are 6 built-in types same like JAVA,

  • Byte
  • Short
  • Int
  • Long
  • Float
  • Double

2. Character Type

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.

See also  6 Future App Technologies which will rule the market in 2024

In Java, you can do something like:

char letter = 65;

However, the following code gives an error in Kotlin.

var letter: Char = 65  // Error

3. Booleans

The Boolean data type has two possible values, either true or false.

val flag = true

4. Array

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)

Null Safety

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

Conditionals

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

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

While loop executes a block of code repeatedly as long as a given condition is true.

while(condition) {
// code to be executed
}

For Loop

A for-loop is used to iterate through ranges, arrays, collections, or anything that provides an iterator.

See also  How to Implement Paging 3 Library in Android Kotlin?

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.

hire dedicated developers cta

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.

lets start your project

Related Articles