Skip to content

Instantly share code, notes, and snippets.

View kalaiselvan369's full-sized avatar
🎯
Focusing

Kalaiselvan kalaiselvan369

🎯
Focusing
View GitHub Profile
@kalaiselvan369
kalaiselvan369 / using_in.kt
Created June 8, 2023 11:24
Checking for membership
fun main() {
val n = 130
if(n in 100..200)
println("$n is a member of 100 to 200")
inFloatRange(4.5)
}
@kalaiselvan369
kalaiselvan369 / for_loop_statement.kt
Created June 8, 2023 11:26
Statement producing side effect
fun main() {
var j = false
for (i in 1..112) {
if (i == 10) {
println(true)
j = true // side effect
}
}
}
@kalaiselvan369
kalaiselvan369 / class_dog.kt
Created June 9, 2023 05:34
Class definition
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
@kalaiselvan369
kalaiselvan369 / constructor.kt
Created June 9, 2023 05:37
Constructor definition
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())
@kalaiselvan369
kalaiselvan369 / access_modifiers.kt
Created June 9, 2023 05:41
Top level class, function and property visibility
// 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) {
@kalaiselvan369
kalaiselvan369 / Api.kt
Last active June 9, 2023 07:13
Importance of package
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
@kalaiselvan369
kalaiselvan369 / User.kt
Created June 9, 2023 07:40
Public Modifier usage
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() {
@kalaiselvan369
kalaiselvan369 / driver.kt
Created June 9, 2023 07:43
Protected modifier usage
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
}
@kalaiselvan369
kalaiselvan369 / named_default_args.kt
Created June 12, 2023 11:12
Named and default arguments
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) {
@kalaiselvan369
kalaiselvan369 / string_template.kt
Created June 12, 2023 11:17
String Template Usage
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}"