As we know that every programming language provide own keyword to perform specific task by compiler or interpreter. There are four types of keyword in Kotlin :
- Hard keywords
- Soft Keywords
- Modifiers Keywords
- Special Keywords
as | break | class | continue | do |
else | false | for | fun | if |
in | interface | is | null | object |
package | return | super | this | throw |
true | try | typealias | typeof | val |
var | when | while |
by | catch | constructor | delegate | dynamic |
get | import | init | param | property |
receiver | set | setparam | where |
actual | abstract | annotation | companian | const | crossinline |
data | enum | expect | external | final | infix |
inline | inner | internal | lateinit | noninline | open |
operator | out | override | private | protected | public |
reified | sealed | suspend | tailrec | vararg |
In Kotlin there are two types of variable basically :
1. Mutable variable
2. Immutable variable
1. Mutable Variable : A mutable variable value can be changed. It works as a non constant variable or can be modifiable variable. In mutable variable we can use var keyword to declare a mutable variable.
Example :
fun main(args : Array<String>){
var name=”deep singh”
print($name)
name=”ramesh” // can be modified or mutable
print($name)
}
2. Immutable variable : An immutable variable value can never changed. It works as a constant variable or read only. In immutable variable we use val keyword to declare immutable variable.
Example :
fun main(args :Array<String>){
val name=”deep singh”
print($name)
name=”ramesh” //compile time error because immutable variable can not be modified
}
What is Variable :
Variable is an identifier that is used to hold value.
Example :
var name=”deep singh”
Or
var name:String=”deep singh” //in this case we can define its type explicit
Or
var name:String
name=”deep singh” // declare first and initialize later
Where var define mutable variable name is an identifier or variable name and “deep singh” is a value which is store by name variable.
Kotlin Data Types :
There are following data type in Kotlin :
1. Numbers
2. Booleans
3. Characters
4. Strings
5. Arrays
1. Numbers : There are following type of Numbers in Kotlin :
i) Byte
ii) Short
iii) Int
iv) Long
v) Float
vi) Double
i) Byte : It has 1 byte size that means 8 bit allocation in memory. Its can hold data between
-128 to 127.
fun main(args: Array<String>) {
var num: Byte = 90
println("$num");
}
-32768 to 32767.
fun main(args: Array<String>) {
var num: Short = 90
println("$num");
}
-2^31 to 2^31.
fun main(args: Array<String>) {
var num: Int = 90
println("$num");
}
fun main(args: Array<String>) {
var num = 90
println("$num");
}
Note if you not define type it automatic take as Int.
-2^63 to 2^63.
fun main(args: Array<String>) {
var num = 90909070907
println("$num");
}
It takes by default long if digit is more than Int size.
fun main(args: Array<String>) {
var num = 90L
println("$num");
}
v) Float : It has 4 byte size that means 32 bit allocation in memory.
fun main(args: Array<String>) {
var num = 90.6F
println("$num");
}
fun main(args: Array<String>) {
var num = 90.6
println("$num");
}
Note It takes by default Double.
vi) Double : It has 8 byte size that means 64 bit allocation in memory.
fun main(args: Array<String>) {
var num = 90.6
println("$num");
}
Note It takes by default Double.
If you want to used to store any type of above data then
you can use Number.
fun main(args: Array<String>) {
var num : Number = 90.6
println("$num");
}
2. Boolean : It supports two type of values : True or False
fun main(args: Array<String>) {
var status = true
if(status)
print(“success”)
else
print(“fail”)
}
3. Character
: It is used to hold single character inform of single quotes.
We can
declare character like below:
var s = ’a’
Or
var
c : Char = ’b’
Special characters can be escaped using
a backslash. For example,
\t, \b, \n, \r
We can also have Unicode character like below :
‘\u0000’
fun main(args: Array<String>) {
vara : Char = ‘b’
println("$a");
}
Or
fun main(args: Array<String>) {
var b = ’t’
println("$b");
}
They can not hold number
fun main(args: Array<String>) {
var b=12
println("$b");
}
There is compile time error.
4. String
: Stings are immutable that can have group of character. And that character can
be accessed based on index.
String
always define in double quotes like “Hello World”.
You can
iterate string like that :
fun main(args: Array<String>) {
For(s in
values)
Print(s)
}
In Kotlin
String represent two type of literals :
1.Escape
String :that can have escaped string like below :
fun main(args: Array<String>) {
var
s=”Hello \n Kotlin”
Print(s)
}
2.Raw String : It can have newlines and arbitrary text in them. It is delimited by using triple quotes (“””). It contains no escaping.
fun main(args: Array<String>) {
var data=”””
Hello Friends
How are you ?
“””
Print(data)
}
5. Array : It
is a predefine class that can have similar type of Objects. It has get and set
functions, and size property, along with other useful member functions
We can
create an array in kotlln following two way :
a. Using Library Function.
1. using this predefine function arrayOf(11,12,13,44,55)
creates an array of integer that has value 11,12,13,44 and 55 in it.
2. arrayOfNulls() library function can be used to
create an array of given size that are initialised with null values.
fun main(args: Array<String>) {
var data=arrayOf(1,11,22,33,44)
for(num in data)
print(num)
}
b. Using Factory Function.
using factory function that takes size of array and the function that
can return initial value of each element, present in the array, given it’s
index.
// Creates an Array with values [“0”, “2”, “4”, “6”, “8”,”10”]
fun main(args: Array<String>) {
var data = Array(6, { i -> (i + i).toString() })
for(num in data)
print(num)
}
Important
points about Array
You can not
assign Array to an Array. This way Kotlin reduces the possible
runtime error.
You can represent arrays of primitive types without boxing overhead For example, ByteArray (to represent array of Byte), IntArray (to represent array of Int) or ShortArray (to represent array of Short) etc. These classes are not inherited from Array class but they have same set of methods and properties. For example,
fun main(args : Array<String>) { |
val data: IntArray =
intArrayOf(21, 13, 11) |
data[0] = data[1] + data[2] |
println(data[0]) |
} |
No comments:
Post a Comment