Variable decleartion ------------------------->>
we have main function first of all
fun main (){
print("hello world")
}
here 'fun' defining a function whose name is main
print("text_here") use to print text in kotline
we can define variable by keyword var which are changable
fun main(){
var myName = "DDaku"
myName = "Suppandi"
print("Hello "+ myName)
}
output will be : Hello Suppandi
Now variable declare by keyword val are cannot be changed for example
fun main(){
val myName = "DDaku"
myName = "Suppandi" ::::: will throw an error : val can not be reassigned
}
comment :: // this is a comment nothing to do with logic and code but covers only single line
:: /* this is a multiline comment also have nothing to do with
but can spread to multiple lines */
Data Types ------------------------------>>
variable types : number type
var myName = "Suppandi" // type string
var myAge = 31 // type int 32 bit
var myByte : Byte = 13 // 8but
var myShort ; Short= 125// 16bit
var myInt : Int = 19 // 32 bit
var myLong : Long= 100000000000 // 64 bit
val myFloat : Float = 13.37 //default is double by giving Float we assign it a float
val myDouble : Double = 3.1415155556
Variable type boolean :
var isSunny : Boolean = true
isSunny = false
//character type
val letterChar = 'A'
val digitChar = '1'
// string type
val myStr = "Hello World"
val fristCharInStr = myStr[0]
val lastCharInStr= myStr[myStr.length - 1]
print("first Character of myStr string is " + firstCharInStr)
print("Last Character of myStr string is " + lastCharInStr)
String Manipulation ------------------------------------->>
print("first Character of myStr string is $firstCharInStr")
print("Last Character of myStr string is $lastCharInStr")
print("\n Length of myStr is ${myStr.length}")
Arithmatic Operators ----------------------------------->>
var resutl = 5+3 ;
result *=2
print(result)
Comparison operator --------------------------------------->>
val isEqual = 5==4
println("isEqual is $isEqual ")
var myNum =5
myNum +=3
myNum *=4
println("myNum is $myNum")
increment and decrement operator
println("myNum is ${myNum++}")
println("myNum +2 is ${++myNum}")
If else conditions _--------------------------------------------->>
val age = 17
if(age>=21){
println(" now you may drink in the US")
}
else if(age >=18){
println("you may vote now ")
}
else if(age >=16) {
println("you may drive now ")
}
else {
print("you are too young to die")
}
if statments include a boolean variable inside it if true then execute other wise else statment gets the controle
Switch Case In Kotline ------------------------------------------>>
Switch Statments :are much faster than if-else ledder
var season = 3
when(season){
1 -> println("Spring")
2 -> println("Summer")
3 -> { println("fall")
println("Autmn") }
4 -> println("winter")
else -> println("Invalid Season")
}
another way of switch case logic in kotline
var month =3
when(month){
in 3..5 -> println("Spring")
in 6..8 -> println("Summer")
in 9..11-> println("Autmn")
12,1,2 -> println("winter")
}
A very good example to go through for switch case type in kotline
var x : Any = 13.37f
when(x){
is Int -> println("$x is an Int ")
is Float -> println("$x is a float ")
is Double -> println("$x is a double ")
is Char -> println ("$x is a character ")
is String -> println ("$x is a string ")
else -> println("$x is something which is not char , int , float , double , string")
}
//---LOOPS ---------------------------------->>
while loop
var x =1
while ( x<=10){
println("$x")
x++
}
do while loops
do{
print("$x")
x++
}while( x<10)
for loops
for (num in 1..10){
print("$num")
}
for ( i in 1 until 10) {
print("$i ")
}
for (i in 10 down to 1 step 2){
print("$i ")
}
The BREAK and CONTINUE statments --------------------->>
for(i in 1 untill 20){
print("$i ")
if( i/2 == 5 ){
continue // telling theme to continue without listing to the code below it , ie start next iteration while break will ends this loop right here when condition is met
}
print("$i ")
}
println("Done with the loop")
Creating New Fuctions ----------------------->>
fun function_name ( arguments of that function ) : return_type{
//code inside
}
fun addition ( x:Int , y:Int ) : Int {
return (x+y)
}
fun main(){
var a =1
var b =2
var add
add = addition( a, b )
println("addition of a and b is $add ")
}
// Nullables in Kotline
null reference ,
fun main(){
var name :String = "Suppandi"
// name = null
var nullableName : String? = "Ddaku"
nullableName = null
var lengthOfName = name.length
if( nullableName != null){
var len2 = nullableName.length
}else{
null
}
the above nullable code is same as the statment below
var len2 = nullableName?.length
so
println(nullableName?.toLowerCase())
if empty return null
if have a string in it return it with all letters to lowercase if possible
Elvis Operator :------------------>>
val name = nullableName ?: "Guest"
means name contains value insite nullableName variable have stored in unless it is null
if it is null then the value will going to store in name variable is "Guest"
elvis operator is ?:
nullableName!!.tolowercase() // if we are sure that nullableName is not Null
example
var name = "Suppandi"
var nullableName : String? ="Devesh"
nullableName = null
println(" hello $name \n hello ${nullableName?:"DDaku"} ") / /will print hello Suppandi (next line) hello DDaku as nullableName is null
Comments
Post a Comment