Variables:In Kotlin, everything is an object in the sense that we can call member functions and properties on any variable. Some of the types can have a special internal representation - for example, numbers, characters and Boolean can be represented as primitive values at runtime - but to the user they look like ordinary classes. In this section we describe the basic types used in Kotlin: numbers, characters, boolean, and strings.
In Kotlin val and var both are used to declare a variable.
var : It is like general variable and its known as a mutable variable in kotlin and can be assigned multiple times.
variables defined with var are mutable(Read and Write).
var x: Int=100
var a = 7 //An Int
var b:Boolean=true
var y: Double = 21.5
var s: String = "my String"
You can declare it anytime and initialized any were.
var value:Int //you can declare it anytime
value=100 //initialized here
You can change value at any time:
value=20
val : It is like constant variable and its known as immutable in kotlin and can be initialized only single time.Like final variables in JAVA.
variables defined with val are immutable(Read only).
val language = "English"
language = "Hindi" // Error
As needed we Can do an explicit casting:As a consequence, smaller types are NOT implicitly converted to bigger types. This means that we cannot assign a value of type Byte to an Int variable without an explicit conversion
val b: Byte = 1 // OK, literals are checked statically
val i: Int = b // ERROR
We can use explicit conversions to widen numbers
val i: Int = b.toInt() // OK: explicitly widened
Every number type supports the following conversions:
toByte(): Byte
toShort(): Short
toInt(): Int
toLong(): Long
toFloat(): Float
toDouble(): Double
toChar(): Char
In Kotlin val and var both are used to declare a variable.
var : It is like general variable and its known as a mutable variable in kotlin and can be assigned multiple times.
variables defined with var are mutable(Read and Write).
var x: Int=100
var a = 7 //An Int
var b:Boolean=true
var y: Double = 21.5
var s: String = "my String"
You can declare it anytime and initialized any were.
var value:Int //you can declare it anytime
value=100 //initialized here
You can change value at any time:
value=20
val : It is like constant variable and its known as immutable in kotlin and can be initialized only single time.Like final variables in JAVA.
variables defined with val are immutable(Read only).
val i = 20 // An Int
val iHex = 0x0f // An Int from hexadecimal literal
val l = 9L // A Long
val d = 9.5 // A Double
val f = 9.5F // A Float
val s = "Example" //String
You can't change value:
language = "Hindi" // Error
As needed we Can do an explicit casting:As a consequence, smaller types are NOT implicitly converted to bigger types. This means that we cannot assign a value of type Byte to an Int variable without an explicit conversion
val b: Byte = 1 // OK, literals are checked statically
val i: Int = b // ERROR
We can use explicit conversions to widen numbers
val i: Int = b.toInt() // OK: explicitly widened
Every number type supports the following conversions:
toByte(): Byte
toShort(): Short
toInt(): Int
toLong(): Long
toFloat(): Float
toDouble(): Double
toChar(): Char
No comments:
Post a Comment