Skip to content

Instantly share code, notes, and snippets.

View arsalankhan994's full-sized avatar

Erselan Khan arsalankhan994

View GitHub Profile
fun main() {
val methodInKotlin = MethodInKotlin()
methodInKotlin.methodWithoutReturnTypeAndNoParameter()
val first = methodInKotlin.methodWithReturnTypeAndNoParameter()
println("return type of methodWithReturnTypeAndNoParameter: $first")
methodInKotlin.methodWithoutReturnTypeWithParameter("Erselan Khan")
val second = methodInKotlin.methodWithReturnTypeWithParameter("Erselan Khan")
println("return type of methodWithReturnTypeWithParameter: $second")
}
fun main() {
// Type Checking
// Initializing list with String, Int and Boolean value
val list = listOf<Any>("Erselan Khan", 21, false)
// loop on above list
list.forEach {
when(it) {
is String -> println("String value is: $it")
fun main() {
// Smart Casting
var studentWithRealObject: Any? = Student()
if (studentWithRealObject is Student) {
println("Student name: ${studentWithRealObject.name}")
println("Student age: ${studentWithRealObject.age}")
}
fun main() {
// Non changeable values in Kotlin
val personAge = 27
/*personAge = 26*/ // we can not reassign value to val variable
// Changeable values in Kotlin
var name = "Erselan Khan"
name = "Khan Erselan"
main() {
/*
object classes example
*/
val singleton = SingletonClass.age
println(singleton)
SingletonClass.updateAge(20)
println(SingletonClass.age)
/*
Using open keyword to inherit this class
*/
open class InheritableClass {
}
/*
Try to Inherit inheritable class
*/
// we don't need to add open keyword
abstract class AbstractClasses() {
abstract fun someAbstractMethod()
}
class SomeClassName: AbstractClasses() {
override fun someAbstractMethod() {
}
fun main() {
// Multi Condition in Kotlin using when/else
val list = listOf("Erselan Khan", 27, "arsalankhan994@gmail.com")
when(list[(list.indices).random()]) {
"Erselan Khan" -> println("name is ${list[0]}")
27 -> println("age is ${list[1]}")
"arsalankhan994@gmail.com" -> println("email is ${list[2]}")
main() {
/*
Data class example
*/
val dataClass = StudentDataClass()
dataClass.copy()
dataClass.toString()
}
/*
main() {
/*
Class bound methods and variable
*/
val someVariable = ClassBoundMethod
someVariable.classBoundMethod()
someVariable.name
}