Skip to content

Instantly share code, notes, and snippets.

@kalaiselvan369
Created June 12, 2023 11:27
Show Gist options
  • Save kalaiselvan369/4b05c87d173222c76ca7f6174c5bae2e to your computer and use it in GitHub Desktop.
Save kalaiselvan369/4b05c87d173222c76ca7f6174c5bae2e to your computer and use it in GitHub Desktop.
Data classes
package data_classes
data class Machine(val name: String)
enum class ShipType {
CRUISE,
WAR,
GOODS
}
data class Ship(val type: ShipType = ShipType.CRUISE) {
var safetyBoats = 0
}
data class User(val name: String = "Virat", val age: Int = 35, val nationality: String = "India")
fun main() {
val machine1 = Machine("Fan")
val machine2 = Machine("Fan")
println(machine1) // prints readable format of class instance of object address
//structural equality check
println(machine1 == machine2) // true - content equality check
//referential equality check
println(machine1 === machine2) // false object address equality check
val ship1 = Ship()
ship1.safetyBoats = 4
val ship2 = Ship()
ship2.safetyBoats = 2
println(ship1) // only ship type is printed, safetyBoats count will not be printed since it is inside the body
println(ship1 == ship2) // should be false but print true. equality check is not performed for member properties
println(ship1 === ship2)
// Destructuring declarations
val (name, age, nationality) = User()
println("$name, $age, $nationality")
val (_, _, n) = User()
println(n)
val user = User(name = "Dhoni", age = 40)
println("${user.component1()}, ${user.component2()}, ${user.component3()}")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment