Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing

Learn kotlin Variables


Sure! Variables in Kotlin are used to store data that can be changed or manipulated during the execution of a program. Here's how you can use variables in Kotlin:

Declaring Variables

In Kotlin, you can declare variables using either the var keyword (mutable variables) or the val keyword (immutable variables).

Example

// Mutable variable (can be changed)
var myNumber = 10
myNumber = 20 // Valid

// Immutable variable (cannot be changed)
val PI = 3.14
// PI = 3.14159 // Error: Val cannot be reassigned

Data Types

Kotlin supports various data types such as integers, floating-point numbers, characters, booleans, and strings. You can let Kotlin infer the data type, or you can specify it explicitly.

Example

// Type inference
var age = 25 // Kotlin infers age as Int
var name = "John" // Kotlin infers name as String

// Explicit data type declaration
var weight: Double = 68.5
var isStudent: Boolean = true

Printing Variables

You can print the value of variables using the println() function.

Example

var score = 85
println("The score is $score") // Output: The score is 85

Conclusion

Variables are essential components of any programming language as they allow you to store and manipulate data dynamically. In Kotlin, you can declare variables using var for mutable data and val for immutable data. Additionally, Kotlin supports various data types, and you can print the values of variables using println().



Advertisement





Q3 Schools : India


Online Complier

HTML 5

Python

java

C++

C

JavaScript

Website Development

HTML

CSS

JavaScript

Python

SQL

Campus Learning

C

C#

java