Kotlin Variables Example

 Basics    Aries, Fire, Mars
20 Comments   Aug 20th 2012


In this tutorial you will learn about Variables via simple step by step examples.

Example 1: Variables

Let us look at a simple Variables example written in Kotlin Programming Language. Follow the following steps:

Step 1: Create Project

  1. Open your favorite Kotlin IDE.
  2. In the menu go to File --> Create New Project.

Step 2: Add Dependencies

No dependencies are needed for this project.

Step 3: Write Code

Our code will comprise the following Kotlin files:

  • Variables.kt
  1. In your editor or IDE, create a file known as Variables.kt.
  2. Then add the following code:

(a). Variables.kt

package Variables

//There are two type of variable declarations in kotlin, mutable and immutable.
//use var declaration for mutable variables and val for immutable variables
fun main(args : Array<String>) {
    var message = "var means a mutable variable so you can change it. "
    println("'message' variable has this value '$message'")
    message = "wallah, modified"
    println("now 'message' has this value '$message'")

    println("")
    val forever = "val means that the variable is immutable. If you try to modify this 'forever' variable, the compiler will complain."
    println(forever)
    //immutable = "oi" -- bzz, wrong!
}

Step 4: Run

Copy the code, build and run.

Reference

Here are the reference links:

Number Link
1. Download Example
2. Follow code author
3. Code: Apache 2.0 License

Leave a comment


  • John

    9:41 PM on August 24, 2013

    I don't believe in astrology but still your writing style is really great!

  • John

    9:41 PM on August 24, 2013

    I don't believe in astrology but still your writing style is really great!