This file contains 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 data_classes | |
data class Machine(val name: String) | |
enum class ShipType { | |
CRUISE, | |
WAR, | |
GOODS | |
} |
This file contains 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 a: Int? = null | |
println(a) | |
var b: String? = "Hi" | |
// !! means non null assertion | |
for (char in b!!) { |
This file contains 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 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() |
This file contains 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 using_when | |
import kotlin.random.Random | |
fun main() { | |
val a = Random.nextInt(2) | |
when (a) { | |
2 -> println("even") |
This file contains 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() { | |
"Hi".singleQuote() | |
"Hello".strangeQuote() | |
val player = Player(name = "Sachin", team = "India") | |
player.hobbies(player.name) | |
} | |
fun String.singleQuote() = "'${this}'" | |
fun String.doubleQuote() = "\"${this}\"" |
This file contains 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
import java.sql.Date | |
fun main() { | |
exploreRangeTo() | |
val scoreBoard = ScoreBoard() | |
scoreBoard + 6 | |
println(scoreBoard.displayScore()) | |
} |
This file contains 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}" |
This file contains 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 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 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() { |
NewerOlder