4. Kotlin is a relatively new programming language
developed by JetBrains for modern multiplatform
applications. Nowadays, Kotlin is widely used for Android
development instead of Java. It is because Kotlin is safe,
concise, and fun to read and write.
9. • Kotlin is a relatively new programming language developed by JetBrains for
modern multiplatform applications. Nowadays, Kotlin is widely used for Android
development instead of Java. It is because Kotlin is safe, concise, and fun to
read and write.
10. About Kotlin Programming
•Open Source - The Kotlin compiler, Intellij IDEA plugin, and build tools are all open source.
•Interoperable - Kotlin is 100 percent interoperable with Java. This means all your current
Java/Android code works seamlessly with Kotlin.
•Concise - Compared to Java, Kotlin code are much more concise. Also, Kotlin code is much
more expressive (easier to understand and write).
•Tool-friendly - Kotlin is developed by JetBrains, the company renowned for creating
development tools. You can choose any Java IDE to write Koltin code.
Why Learn Kotlin?
•Kotlin is 100 percent interoperable with Java. Hence your Java/Android code works with
Kotlin.
•Kotlin allows you to cut off the lines of code by approximately 40% (compared to Java).
•Learning Kotlin is easy. It is particularly easy if you already know Java.
•Kotlin is tool-friendly. You can use any Java IDE or command line to run Kotlin.
11. Overview of Basic Syntax
• Function
• Variable
• Class
• Constructors
• Comments
13. Variable
/*
* This is main function. Entry point of the application.
* */
fun main(args: Array<String>) {
var myNumber = 10 // Int
var myDecimal = 1.0 // Float
var isActive = true // Boolean
var myString: String // Mutable String
myString = "Hello World"
myString = "Another World"
val myAnotherString = "My constant string value" // Immutable String
// myAnotherString = "some value" // NOT ALLOWED, since it is immutable
print(myNumber)
• }
15. fun main(args: Array<String>) {
var name: String
name=“ABC”
//print (name) // instead of printing name in main function lets print
through user define function
display(name)
}
fun display(name: String) //Parameterized Function
{
print(“Name is”+name)
}
16. fun main(args: Array<String>) {
var name: String
name=“ABC”
var p1= Person()
p1.display(name)
}
class Person { // Class
fun display(name: String) //Parameterized Function
{
print(“Name is”+name)
}
}
17. fun main(args: Array<String>) {
var p1= Person()
p1.name=“ABC”
p1.display(p1.name)
}
class Person { // Class
fun display(name: String) //Parameterized Function
{
var name: String= “ “
print(“Name is”+name)
}
}
18. fun main(args: Array<String>) {
var p1 = Person()
p1.name = “ABC”
print("The name of the person is ${p1.name}")
}
class Person {
var name: String = ""
}
19.
20.
21.
22. /*
* Class, Primary Constructor, Secondary Constructor and Init Block
* */
fun main(args: Array<String>) {
var student = Student("Steve", 10)
println(student.id)
}
class Student(var name: String) {
var id: Int = -1
init {
println("Student has got a name as $name and id is $id")
}
constructor(n: String, id: Int): this(n) {
// The body of the secondary constructor is called after init block
this.id = id
}
}
23. fun main(args: Array<String>) {
var dog = Dog()
dog.bread = "labra"
dog.color = "black"
dog.bark()
dog.eat()
var cat = Cat()
cat.age = 7
cat.color = "brown"
cat.meow()
cat.eat()
var animal = Animal()
animal.color = "white"
animal.eat()
}
open class Animal {
var color: String = ""
fun eat() {
println("Eat")
}
}
class Dog : Animal() {
var bread: String = ""
fun bark() {
println("Bark")
}
}
class Cat : Animal() {
var age: Int = -1
fun meow() {
println("Meow")
}
}
24. fun main(args: Array<String>) {
var dog = MyDog()
println(dog.color)
dog.eat()
}
open class MyAnimal {
open var color: String = "White"
open fun eat() {
println("Animal Eating")
}
}
class MyDog : MyAnimal() {
var bread: String = ""
override var color: String = "Black"
fun bark() {
println("Bark")
}
override fun eat() {
super<MyAnimal>.eat()
println("Dog is eating")
}
}
25. fun main(args: Array<String>) {
var dog = TheDog("Black", "Pug")
}
open class TheAnimal{
var color: String = ""
constructor(color: String) {
this.color = color
println("From Animal: $color")
}
}
class TheDog : TheAnimal{
var bread: String = ""
constructor(color: String, breed:
String): super(color) {
this.bread = breed
println("From Dog: $color and
$breed")
}
}