Saturday, 25 November 2017

Kotlin: Convert character to Int

Char type provides toInt() method to convert a character to integer.

IntConverter.kt
fun main(args: Array<String>) {

 var myChar : Char = 'a'

 println("Value of myChar is : ${myChar}")
 println("Value of myChar in int is : ${myChar.toInt()}")
}

Output
Value of myChar is : a
Value of myChar in int is : 97


Previous                                                 Next                                                 Home

Friday, 24 November 2017

Kotlin: Characters

'Char; keyword is used to define single character. Character is enclosed in single quotes.

Ex
var myChar : Char = 'a'

HelloWorld.kt
fun main(args: Array<String>) {

 var myChar : Char = 'a'

 println("Value of myChar is : ${myChar}")
}

Output
Value of myChar is : a


All the escape sequences that are supported by Java are also supported by Kotlin.
Previous                                                 Next                                                 Home

Wednesday, 22 November 2017

Kotlin: Numbers

Kotlin provides below data types to support numbers.

Data Type
Size in Bits
Double
64
Float
32
Long
64
Int
32
Short
16
Byte
8

Numbers.kt
fun main(args: Array<String>) {
 var doubleVar: Double = 1.23
 var floatVar: Float = 1.23f
 var longVar: Long = 123
 var intVar: Int = 123
 var shortVar: Short = 123
 var byteVar: Byte = 123


 println("doubleVar : ${doubleVar}")
 println("doubleVar : ${floatVar}")
 println("longVar : ${longVar}")
 println("intVar : ${intVar}")
 println("shortVar : ${shortVar}")
 println("byteVar : ${byteVar}")
}

Output
doubleVar : 1.23
doubleVar : 1.23
longVar : 123
intVar : 123
shortVar : 123
byteVar : 123

Hexa, octal and Binary literal notation
Kotlin support Hexa and binary notation, but not support octal notation. Hexa numbers are represented by prefixing 0x,


Numbers.kt

fun main(args: Array<String>) {
 var hexaNum : Int = 0x123
 var binaryNum : Int = 0b10101010

 println("hexaNum : ${hexaNum}")
 println("binaryNum : ${binaryNum}")
}

Output

hexaNum : 291
binaryNum : 170


You can also represent numbers using ‘_’.


Numbers.kt
fun main(args: Array<String>) {

 var ten = 10
 var hundred = 100
 var thousand = 1000
 var tenThousand = 10_000
 var oneLaksh = 100_000
 var tenLaksh = 1_000_000

 println("ten : ${ten}")
 println("hundred : ${hundred}")
 println("thousand : ${thousand}")
 println("tenThousand : ${tenThousand}")
 println("oneLaksh : ${oneLaksh}")
 println("tenLaksh : ${tenLaksh}")
}


Output
ten : 10
hundred : 100
thousand : 1000
tenThousand : 10000
oneLaksh : 100000
tenLaksh : 1000000



Previous                                                 Next                                                 Home

Tuesday, 21 November 2017

Kotlin: nullable types

By default, kotlin reference types (Int, Float, Double, String etc.,) do not take null values. Kotlin introduced this feature to avoid null pointer exceptions.

Test.kt
fun main(args: Array<String>) {
 var name : String = "Krishna"
 
 name = null
}

When you try to compile above program, kotlin throws below error.
ERROR: Null can not be a value of a non-null type String (6, 9)

How can I define a variable that take null value?
By using nullable types, you can define a variable that takes null values. Append the ‘?’ to the reference type to take null values.

Type
Nullable type
Int
Int?
String
String?
Double
Double?
Person
Person?


Ex
 var id: Int? = 1
 var name: String? = "Krishna"
 var salary: Double? = 123.456
 var person: Person? = Person("Krishna")

 id = null
 name = null
 salary = null
 person = null



Previous                                                 Next                                                 Home

Monday, 20 November 2017

Data types in Kotlin

Java has primitive types like int, float, char etc., what about Kotlin?
In Users point of view, kotlin designed everything as object, but at run time Booleans, numbers and characters are represented as primitive types.

HelloWorld.kt
fun main(args: Array<String>) {
 var doubleVar: Double = 1.23
 var floatVar: Float = 1.23f
 var longVar: Long = 123
 var intVar: Int = 123
 var shortVar: Short = 123
 var byteVar: Byte = 123


 println("doubleVar : ${doubleVar}")
 println("doubleVar : ${floatVar}")
 println("longVar : ${longVar}")
 println("intVar : ${intVar}")
 println("shortVar : ${shortVar}")
 println("byteVar : ${byteVar}")
}

Output
doubleVar : 1.23
doubleVar : 1.23
longVar : 123
intVar : 123
shortVar : 123
byteVar : 123


Previous                                                 Next                                                 Home

Saturday, 18 November 2017

Define variables in Kotlin

Variable is a named memory location used to store data.

Syntax
var variableName : DataType = value
val variableName : DataType = value

Both ‘var’ and ‘val’ are the keywords.

Ex
var a : Int  =10;
‘a’ is of type int and assigned with value 10.

val b : Double = 1.23
‘b’ is of type Double and assigned with value 1.23. The keyword ‘val’ is used to define constants (immutable objects).


HelloWorld.kt

fun main(args: Array<String>) {
 var a : Int  =10;
 val b : Double = 1.23
 
 println("a : ${a}")
 println("b : ${b}")
 
}


C:\>kotlinc HelloWorld.kt

C:\>kotlin HelloWorldKt
a : 10
b : 1.23


Previous                                                 Next                                                 Home

Friday, 17 November 2017

Brainfuck: Double a number

Below snippet double the number 3.
+++                #Initialize the cell with number
[
 >                 # Move to next cell
 ++                # Increment the cell by 2
 <                 # Move to previous cell
 -                 # Decrement the cell value by 1
]                  # Repeat this step until the first cell is 0
>.                 # Go to second cell and print the value

What if you want to read a number from user and double it. Procedure is straight forward. First you read the number, Since the number stored in ASCII format, convert the ASCII value to decimal. Follow above same steps.


Below logic double the number.
,                #Initialize the cell with number

# Logic to convert ASCII numbers to their decimal format
>
++++++++         # Init second cell to 8 
[
 -
 <
 ------
 >
]

# Double the number
<
[
 >                 # Move to next cell
 ++                # Increment the cell by 2
 <                 # Move to previous cell
 -                 # Decrement the cell value by 1
]                  # Repeat this step until the first cell is 0


Previous                                                 Next                                                 Home