Lesson 4: Types of variables in Kotlin language

Lesson 4: Types of variables in Kotlin language

Hello viewer, You can now have our Tutorial Lessons in your android mobile device and read it offline.
Download kotlin Programming APP on PlayStore
Download Website SEO Lessons APP on PlayStore

A variable is a shell where there is a value. It consists of a name and belongs to a type.

In the Kotlin language if we need to store an integer value we can define a variable of type:

 Byte
 Shorts
 Int
 Long

Depending on the maximum value to be stored, we will use one of these four types of integer variables. For example in a variable of type Int we can store the maximum value: 2147483647 and in general we have:

Topic: Types of variables in Kotlin Programming language

Types of variable
                                   Minimum                          Maximum
Byte                                 -128                            +127
Short                             -32 768                         +32 767
Int                        -2 147 483 648                  +2 147 483 647
Long           -9 223 372 036 854 775 808      +9 223 372 036 854 775 807
If we have to store a value with a decimal part (ie with comma as can be 3.14) we must use a variable of type:
 Double
 Float

The Double type is more accurate than the Float type.

And another type of variables that we will use in our first exercises will be the variables of type String that allow to store a set of characters:

 String

A variable in Kotlin can be immutable, this means that when we assign a value it can not change more along the program, or it can be mutable, that is to say that it can change the data stored during the execution of the program.

Topic: Types of variables in Kotlin Programming language

To define a variable in Kotlin immutable we use the keyword val, for example:

     val age: Int
     age = 48
     val salary: Float
     salary = 1200.55f
     val total: Double
     total = 70000.24
     val title: String
     title = "Sales System"

We have defined four variables and assigned their respective values.

Once we assign a value to an immutable variable its contents can not be changed, if we try the compiler will generate an error:

     val age: Int
     age = 48
      age = 78 

If we compiled it appears an error since we are trying to change the content of the variable age that has a 48. As we define it with the keyword val means that it will not be changed during the entire execution of the program.

In other situations we need the variable to change the stored value, for this we use the keyword var to define the variable:

     var month: Int
     month = 1
     // some more lines of code
     month = 2

The variable month is of type Int and stores a 1 and then at any other time of the program we can assign another value.

-Kotlin Work Problem

Create a program that defines two immutable variables of type Int. Then define a third variable that stores the sum of the first two variables and displays them. Then store in the variable the product of the first two variables and show the result.

Topic: Types of variables in Kotlin Programming language

Perform the same steps as above to create a project and create the Main.kt file where to encode the respective program (If we have opened the IntelliJ IDEA we can create a new project from the options menu: New -> Project)

Project2 – Coding180Project.kt

fun main (parameter: Array <String>) {
//coding180.com
     val value1: Int
     val value2: Int
     value1 = 100
     value2 = 400
     var result: Int
     result = value1 + value2
     println ("The sum of $value1 + $value2 is $result")
     result = value1 * value2
     println ("The product of $value1 * $value2 is $result")
 }

We define and initialize two immutable Int variables (we use the val keyword):

val value1: Int
     val value2: Int
     value1 = 100
     value2 = 400

We define a third variable mutable also of type Int:

var result: Int

First in the result variable we store the sum of the contents of the variables value1 and value2:

var result: Int
     result = value1 + value2

To display the contents of the $result variable in the Console, we use the println function and within the String that shows where we want the contents of the variable to appear, we precede the $character:

println ("The sum of $value1 + $value2 is $result")

That is to say in the Console appears:

The sum of 100 + 400 is 500

As the result variable is mutable we can now store the product of the first two variables:

result = value1 * value2
     println ("The product of $value1 * $value2 is $result")

Kotlin replaces all variables by their contents in a String.

Topic: Types of variables in Kotlin Programming language

The result of the execution of this program will be:

Lesson 4: Types of variables in Kotlin language - Lesson 4: Types of variables in Kotlin language - Lesson 4: Types of variables in Kotlin language - Types of variables in Kotlin

Concise

If we go to the official website of Kotlin we can see that one of its premises is that a program in Kotlin is “CONCISO” (that is to say that an algorithm is expressed in the shortest possible way)

We will make a first change to Project2 to be more concise:

fun main (parameter: Array <String>) {
//coding180.com
     val value1: Int = 100
     val value2: Int = 400
     var result: Int = value1 + value2
     println ("The sum of $value1 + $value2 is $result")
     result = value1 * value2
     println ("The product of $value1 * $value2 is $result")
 }

In this first change we can observe that in Kotlin we can define the variable and immediately assign its value. We can assign a literal value such as 100:

     val value1: Int = 100

Or the content of other variables:

     var result: Int = value1 + value2

Another step that we can give in Kotlin to make our program more concise is not to indicate the type of data of the variable and to make the Kotlin compiler infer:

fun main (parameter: Array <String>) {
//coding180.com
     val value1 = 100
     val value2 = 400
     var result = value1 + value2
     println ("The sum of $value1 + $value2 is $result")
     result = value1 * value2
     println ("The product of $value1 * $value2 is $result")
 }

The result of compiling this program is the same as the previous ones. The Kotlin compiler when we do:

     val value1 = 100

Deduces that we want to define a variable of type Int

If in the variable value1 we store the number 5000000000, then the Kotlin compiler can infer that a variable of type Long must be defined

     val value1 = 5000000000

To work with the decimal values ​​by inference we must use the following syntax:

     var weight = 4122.23 // infer that it is Double
     val height = 10.42f // we must add the f to the end to be a Float and not a Double    

Very easy is to define a String:

     val title= "Billing System"

We will use this syntax extensively throughout the tutorial.

Topic: Types of variables in Kotlin Programming language

We will use this syntax extensively throughout the tutorial.

Problems proposed

  • Define an immutable variable with the value 50 that represents the side of a square, in another two immutable variables store the surface and the perimeter of the square.
    Display the surface and perimeter of the Console.
  • Define three immutable variables and load by weights the weights of three people with Float values. Calculate the average weight of people and show it.

Topic: Types of variables in Kotlin Programming language

Solution

 

About The Author

Related posts

Leave a Reply