Skip to content

Instantly share code, notes, and snippets.

View kalaiselvan369's full-sized avatar
🎯
Focusing

Kalaiselvan kalaiselvan369

🎯
Focusing
View GitHub Profile
package data_classes
data class Machine(val name: String)
enum class ShipType {
CRUISE,
WAR,
GOODS
}
@kalaiselvan369
kalaiselvan369 / dealing_null.kt
Last active June 12, 2023 11:24
Dealing null values
fun main() {
val a: Int? = null
println(a)
var b: String? = "Hi"
// !! means non null assertion
for (char in b!!) {
@kalaiselvan369
kalaiselvan369 / enums.kt
Last active June 12, 2023 11:40
Enumeration in kotlin
package enumerations
fun main() {
val east = Direction.EAST
println(east.notation)
println(east.oppositeDirection())
//println(east.opposite()) // not possible, you cannot invoke extension function declared inside class
east.westOpposite()
package using_when
import kotlin.random.Random
fun main() {
val a = Random.nextInt(2)
when (a) {
2 -> println("even")
fun main() {
"Hi".singleQuote()
"Hello".strangeQuote()
val player = Player(name = "Sachin", team = "India")
player.hobbies(player.name)
}
fun String.singleQuote() = "'${this}'"
fun String.doubleQuote() = "\"${this}\""
@kalaiselvan369
kalaiselvan369 / operator_overloading.kt
Created June 12, 2023 11:18
Operator Overloading
import java.sql.Date
fun main() {
exploreRangeTo()
val scoreBoard = ScoreBoard()
scoreBoard + 6
println(scoreBoard.displayScore())
}
@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}"
@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 / 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 / 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() {