Hi All ,
Previously , I posted the overview of Kotlin . Kotlin is fully supported in Android Studio 3.0 and higher.
For Kotlin support , we just need to select the Kotlin language , at the time of configure the new android project in Android Studio.
Now let's take a look at some basics of Kotlin Language :
Output:
Explanation :
Here in this example , we can see that , in second print statement , I have used semicolon(;), at the end of statement . Semicolon (;) is not mandatory at the end of statement in Kotlin.
Another thing , I have used $ , to print the value of variable within the String . Otherwise , we can used + size to append the value with String (same as JAVA).
Previously , I posted the overview of Kotlin . Kotlin is fully supported in Android Studio 3.0 and higher.
For Kotlin support , we just need to select the Kotlin language , at the time of configure the new android project in Android Studio.
Now let's take a look at some basics of Kotlin Language :
=> Declare Variables :
val and var both are used to declare a variable. var is like general variable and it's known as a mutable variable in kotlin and can be assigned multiple times.val is like Final variable and it's known as immutable in kotlin and can be initialized only single time.=>Numbers :
The representation of numbers in Kotlin is pretty similar to Java, however, Kotlin does not allow internal conversion of different data types. Here is one example of my Demo Activity :
class MainActivity : AppCompatActivity() { val intergerValue: Int = 10
val doubleValue: Double = 10.00
val floatValue: Float = 10.00f override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) println("Int Value is "+intergerValue) println("Double Value with string is -> $doubleValue "); println("Float Value is "+floatValue) } }
Int Value is 10
Double Value with string is -> 10.0
Float Value is 10.0
Explanation :
Here in this example , we can see that , in second print statement , I have used semicolon(;), at the end of statement . Semicolon (;) is not mandatory at the end of statement in Kotlin.
Another thing , I have used $ , to print the value of variable within the String . Otherwise , we can used + size to append the value with String (same as JAVA).
=> Characters & Boolean :
Assigning and defining of character and boolean variables should be done like this :
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val letter: Char // defining a variable
letter = 'R' // Assigning a value to it
println("$letter")
val letter1: Boolean // defining a variable
letter1 = true // Assinging a value to it
println("Character value -> "+"$letter1") }
Output:
arrayList.add("Hi")
arrayList.add("Good Morning")
println("Size of ArrayList->"+arrayList.size)
arrayList.add("Hello")R Character value -> true
Explanation:
Here , we can not reassign any new value for that "val" variable .It will throw compile time error:
"Val value can not be reassigned! "
=> String :
var rawString :String ="I am Raw String!\n"println("Hey!!"+rawString)Output:Hey!!I am Raw String!
=> Arrays :
Arrays should be defined like following :
var Arr1 = arrayOf(1,10,4,6,15) var Arr2 = arrayOf<Int>(1,10,4,6,15) var Arr3 = arrayOf<String>("Surat","Mumbai","Rajkot") var Arr4 = arrayOf(1,10,4, "Ajay","Prakesh") var Arr5: IntArray = intArrayOf(5,10,15,20) println("Hey!! I am array Example1->"+Arr1[2]) println("Hey!! I am array Example2->"+Arr2[2]) println("Hey!! I am array Example3->"+Arr3[2]) println("Hey!! I am array Example4->"+Arr4[3]) println("Hey!! I am array Example5->"+Arr5[2])
val arrayList = ArrayList<String>()Output:Hey!! I am array Example1->4 Hey!! I am array Example2->4 Hey!! I am array Example3->Rajkot Hey!! I am array Example4->Ajay Hey!! I am array Example5->15Explanation:
Here , in Arr4 , we can see that we can also define , integer and String in one array also.
=> ArrayList :
arrayList.add("Hi")
arrayList.add("Good Morning")
println("Size of ArrayList->"+arrayList.size)
Output: Size of ArrayList->3
That's it , for this post . In next post we will go through the condition statements and control flows.
No comments:
Post a Comment