Kotlin Tutorial Beginner Guide for Kotlin Programming Language Tutorial

Defining variables

In Kotlin Programming Language,  we can declare variables using following two keywords.

  • val: it is like as a constant or the read-only variable. Also known as immutable in Kotlin Programming Langauge. val can be initialized only single time.
  • var: It is a general way to declare variables. var is also known as a mutable variable in Kotlin Programming Language. var can also be initialized later.

Following are some examples about, How to declare variables in Kotlin Programming Language?

Code

<pre class="brush:java;">
fun main(args:Array&lt;String&gt;)
{
    println(&quot;Hello, vLemonn! Declare Variable Example&quot;)

    /*
    * Immutable Variables
    * */

    val ConstantVal1=&quot;Hello&quot;
    val ConstantVal2=2

    println(&quot;Constant Variable 1 Value: $ConstantVal1&quot;)
    println(&quot;Constant Variable 2 Value: &quot;+ConstantVal2)

    /*
    * Below line throws error
    * Error:(19, 5) Kotlin: Val cannot be reassigned
    * */
    ConstantVal1=&quot;Hello, vLemonn&quot;

    /*
    * Mutable Variables
    * */
    var MyAge=120
    var MyName=&quot;Mayank Sanghvi&quot;

    /*
    * Defiantly my age is not 50 re-assign it.
    * */
    MyAge=18
    MyName=&quot;Mayank&quot;
    /*
    * Now, I am lying about my age. But you can assign mutable variables multiple times.
    * */
}</pre>

<p>&nbsp;</p>

If you noticed in above examples we didn’t include the type of variables. This is how Kotlin is different than other programming languages such as Java and C#, where we need to add variable type at the time of declaration. 

It doesn’t mean that Kotlin is no strongly typed language. Kotlin is a Strongly typed language. But in Kotlin we didn’t need to declare type explicitly. Compiler attempt to figure out the type of an expression from the information available in the expression. This mechanism is Know as Type Inference. Type Inference helps to reduce the boilerplate code.

In computer programming, boilerplate code or boilerplate refers to sections of code that have to be included in many places with little or no alteration. It is often used when referring to languages that are considered verbose, i.e. the programmer must write a lot of code to do minimal jobs.

- Wikipedia

 

Kotlin Programming Langauge Basic Types

 

Numbers: Following are the built-in number types in Kotlin Programming Langauge

  • Long (Width 64
  • Int (Width 32)
  • Short (Width 16)
  • Byte (Width8)
  • Double (Width 64)
  • Float (Width 32)

Following code are shown more examples for using above Data types in Kotlin.

fun main(args:Array<String>)
{
    println("Hello, vLemonn! Declare Variable Example")

    /*
    * Immutable Variables
    * */

    val ConstantVal1="Hello"
    val ConstantVal2=2

    /*
    * We are going to discuss string templates later.
    * */
    println("Constant Variable 1 Value: $ConstantVal1")
    println("Constant Variable 2 Value: "+ConstantVal2)

    /*
    * Below line throws error
    * Error:(22, 5) Kotlin: Val cannot be reassigned
    * */
    ConstantVal1="Hello, vLemonn"

    /*
    * Mutable Variables
    * */
    var MyAge=120
    var MyName="Mayank Sanghvi"

    /*
    * Defiantly my age is not 50 re-assign it.
    * */
    MyAge=18
    MyName="Mayank"
    /*
    * Now, I am lying about my age. But you can assign mutable variables multiple times.
    * */
}

 

Code

<pre class="brush:java;">
val ConstantLong=123456L

    /*
    * We can skip the initialization if we providing the Data Type
    * */
    val ConstantLong2:Long
    ConstantLong2=7890
    val ConstantInteger:Int=124

    /*
    * In IntelliJ Idea for below line you can get a suggest to convert below
    * variable in to a immutable
    * */
    var VariableDouble:Double
    VariableDouble=12.34

    var VariableFloat:Float=12.34F
    VariableFloat = 23.34F

    var VariableShort:Short=12
    VariableShort=23

    var VariableByte:Byte=1

    var VariableHexaDecimal = 0xAB
    var VariableBinary = 0b010</pre>

<p>&nbsp;</p>

In the last two line in above code, we used Hexadecimal and Binary. For hexadecimal values, we need to add 0x as the prefix. Similarly, for binary values, we need to add 0b as a prefix.

Following are the example for Booleans

Code

<pre class="brush:java;">
val ConstantBoolean = false
    if(ConstantBoolean) println(&quot;ConstantBoolean is True&quot;)

    var VariableBoolean:Boolean = true
    if(VariableBoolean) println(&quot;VariableBoolean is True&quot;)
    VariableBoolean = false
    if(!VariableBoolean) println(&quot;VariableBoolean is Not True&quot;)</pre>

<p>&nbsp;</p>

Strings, It’s time to check String in Kotlin Programming Language. For creating Strings we need to use double quotes or triple quotes. In case of triple quotes, we can split the string declaration into multiple lines. Following are the example code for Strings in Kotlin Programming Language.

Code

<pre class="brush:java;">
/*
    * Strings
    * */

    val MyFullName:String=&quot;Mayank Sanghvi&quot;
    var MyWebSite=&quot;https://vLemonn.com&quot;
    MyWebSite = &quot;https://mayanksanghvi.com&quot;

    var AboutME = &quot;&quot;&quot;
        Hello,
        I am Mayank Sanghvi.
        Happy to help others.&quot;&quot;&quot;

    println(MyFullName)
    println(MyWebSite)
    println(AboutME)</pre>

<p>&nbsp;</p>

In Kotlin Programming Language we can define the array by using arrayOf() library function.

val ConstantArray = arrayOf(1,2,3)

What if we need to create an array with a large number of items? In Kotlin we can create an array with an initial size and a function, which used to generate each array item.

var VarArrMultipleOfTwo = Array(6,{i->(i*2)+2 })

Conclusion

In this article, we learned about the basic types available in Kotlin Programing Language. In our next article, we are going to learn about Comments

0 Comments
Leave A Comment

Please login to post your valuable comments.

share