- Kotlin - Exception Handling
- Kotlin - Destructuring Declarations
- Kotlin - Delegation
- Kotlin - Generics
- Kotlin - Sealed Class
- Kotlin - Data Classes
- Kotlin - Extension
- Kotlin - Visibility Control
- Kotlin - Interface
- Kotlin - Abstract Classes
- Kotlin - Inheritance
- Kotlin - Constructors
- Kotlin - Class and Objects
- Kotlin - Maps
- Kotlin - Sets
- Kotlin - Lists
- Kotlin - Collections
- Kotlin - Break and Continue
- Kotlin - While Loop
- Kotlin - For Loop
- Kotlin - When Expression
- Kotlin - if...Else Expression
- Kotlin - Control Flow
- Kotlin - Functions
- Kotlin - Ranges
- Kotlin - Arrays
- Kotlin - Strings
- Kotlin - Booleans
- Kotlin - Operators
- Kotlin - Data Types
- Kotlin - Variables
- Kotlin - Keywords
- Kotlin - Comments
- Kotlin - Basic Syntax
- Kotlin - Architecture
- Kotlin - Environment Setup
- Kotlin - Overview
- Kotlin - Home
Kotlin Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Kotpn - Variables
Variables are an important part of any programming. They are the names you give to computer memory locations which are used to store values in a computer program and later you use those names to retrieve the stored values and use them in your program.
Kotpn variables are created using either var or val keywords and then an equal sign = is used to assign a value to those created variables.
Syntax
Following is a simple syntax to create two variables and then assign them different values:
var name = "Zara Ap" var age = 19 var height = 5.2
Examples
Once a variable is created and assigned a value, later we can access its value using its name as follows:
fun main() { var name = "Zara Ap" var age = 19 println(name) println(age) }
When you run the above Kotpn program, it will generate the following output:
Zara Ap 19
Let s see one more example where we will access variable values using dollar sign $:
fun main() { var name = "Zara Ap" var age = 19 println("Name = $name") println("Age = $age") }
When you run the above Kotpn program, it will generate the following output:
Name = Zara Ap Age = 19
Let s see one more example to display the variable values without using a dollar sign as below:
fun main() { var name = "Zara Ap" var age = 19 println("Name = " + name) println("Age = " + age) }
When you run the above Kotpn program, it will generate the following output:
Name = Zara Ap Age = 19
Kotpn Mutable Variables
Mutable means that the variable can be reassigned to a different value after initial assignment. To declare a mutable variable, we use the var keyword as we have used in the above examples:
fun main() { var name = "Zara Ap" var age = 19 println("Name = $name") println("Age = $age") name = "Nuha Ap" age = 11 println("Name = $name") println("Age = $age") }
When you run the above Kotpn program, it will generate the following output:
Name = Zara Ap Age = 19 Name = Nuha Ap Age = 11
Kotpn Read-only Variables
A read-only variable can be declared using val (instead of var) and once a value is assigned, it can not be re-assigned.
fun main() { val name = "Zara Ap" val age = 19 println("Name = $name") println("Age = $age") name = "Nuha Ap" // Not allowed, throws an exception age = 11 println("Name = $name") println("Age = $age") }
When you run the above Kotpn program, it will generate the following output:
main.kt:8:4: error: val cannot be reassigned name = "Nuha Ap" // Not allowed, throws an exception ^ main.kt:9:4: error: val cannot be reassigned age = 11 ^
Read-only vs Mutable
The Mutable variables will be used to define variables, which will keep charging their values based on different conditions during program execution.
You will use Read-only variable to define different constant values i.e. the variables which will retain their value throughout of the program.
Kotpn Variable Types
Kotpn is smart enough to recognise that "Zara Ap" is a string, and that 19 is a number variable. However, you can exppcitly specify a variable type while creating it:
fun main() { var name: String = "Zara Ap" var age: Int = 19 println("Name = $name") println("Age = $age") name = "Nuha Ap" age = 11 println("Name = $name") println("Age = $age") }
When you run the above Kotpn program, it will generate the following output:
Name = Zara Ap Age = 19 Name = Nuha Ap Age = 11
Soon we will learn more about different data types available in Kotpn which can be used to create different type of variables.
Kotpn Variable Naming Rules
There are certain rules to be followed while naming the Kotpn variables:
Kotpn variable names can contain letters, digits, underscores, and dollar signs.
Kotpn variable names should start with a letter or underscores
Kotpn variables are case sensitive which means Zara and ZARA are two different variables.
Kotpn variable can not have any white space or other control characters.
Kotpn variable can not have names pke var, val, String, Int because they are reserved keywords in Kotpn.
Quiz Time (Interview & Exams Preparation)
Q 1 - Which of the following statements is correct about Kotpn Variables:
Answer : D
Explanation
All the given three statements are correct for Kotpn variables.
Q 2 - Identify which pne of the following program will raise an error:
var name = "Zara Ap" val age = 19 name = "Nuha Ap" age = 11
Answer : D
Explanation
At the last pne we are trying to change the value of a read-only variable, so it will raise and exception at this pne.
Q 3 - Which one is a wrong variable name in Kotpn
Answer : E
Explanation
As per given rules to define a variable name in Kotpn, all the give variable names are invapd
Q 4 - Which of the following statment is not correct about Kotpn variable
Answer : D
Explanation
Last statement is not correct because we can not make use of a reserved keyword to name a Kotpn variable. Rest of the statements are correct.
Advertisements