This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fun main() { | |
| val n = 130 | |
| if(n in 100..200) | |
| println("$n is a member of 100 to 200") | |
| inFloatRange(4.5) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fun main() { | |
| var j = false | |
| for (i in 1..112) { | |
| if (i == 10) { | |
| println(true) | |
| j = true // side effect | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fun main() { | |
| val dog = Dog() | |
| dog.bark() | |
| } | |
| // class is a user defined data type | |
| class Dog { | |
| // characteristics/properties of a class | |
| val legs: Int = 4 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fun main() { | |
| // constructor is a special type of member function that instantiate the class | |
| val elephant = Elephant() | |
| println(elephant) | |
| val employee = Employee("Ram") | |
| println(employee) | |
| val student = Student("Arun") | |
| println(student.toString()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // top level private property | |
| private var score = 0 | |
| // top level private class | |
| private class Batsman() { | |
| fun hitSix() = 6 | |
| } | |
| // top level private function | |
| private fun recordScore(run: Int) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package api | |
| // since the class is declared as internal, it is accessible to all the packages | |
| // that are declared inside the same module. | |
| internal class ApiClient |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class User { | |
| private var userName: String? = null // not accessible outside the class | |
| private var password: String? = null // not accessible outside the class | |
| val displayName: String = "Joey" // accessible outside the class | |
| private val messages = mutableListOf<String>() | |
| private fun resetPassword() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fun main() { | |
| val car = Car() | |
| // although it is protected inside vehicle class while overriding it | |
| // in the subclass we marked as public hence it is accessbile | |
| car.start() | |
| //car.engineStart() // not accessible since it is protected inside the car class | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fun main() { | |
| // we use parameter name while passing arguments to a function. | |
| loadNextPage(page = 1, itemsPerPage = 10) | |
| } | |
| // itemsPerPage parameter by default has its value as 20 | |
| fun loadNextPage(page: Int, itemsPerPage: Int = 20) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fun main() { | |
| // various string template options | |
| val n = 10 | |
| println("value is $n") | |
| val message = "The value is $n" | |
| println(message) | |
| val result = "The multiple is ${n * 8}" |