Kotlinlearncs.online LogoJava

    ← Prev

    Index

    Next →

    Kotlin
    Java
    • Data Modeling 2 : 33

    • Equality and Object Copying : 32

    • Polymorphism : 31

    • Inheritance : 30

    • Data Modeling 1 : 29

    • Companion Objects : 28

    • Encapsulation : 27

    • Constructors : 26

    • Objects, Continued : 25

    • Introduction to Objects : 24

    • Compilation and Immutability : 23

    • Practice with Collections : 22

    • Maps and Sets : 21

    • Lists and Type Parameters : 20

    • Imports and Libraries : 19

    • Multidimensional Arrays : 18

    • Practice with Strings : 17

    • null : 16

    • Algorithms and Strings : 15

    • Strings : 14

    • Functions and Algorithms : 13

    • Practice with Functions : 12

    • More About Functions : 11

    • Errors and Debugging : 10

    • Functions : 9

    • Practice with Loops and Algorithms : 8

    • Algorithms I : 7

    • Loops : 6

    • Arrays : 5

    • Compound Conditionals : 4

    • Conditional Expressions and Statements : 3

    • Operations on Variables : 2

    • Variables and Types : 1

    • Hello, world! : 0

    Equality and Object Copying

    class Person(val name: String) {
    override fun equals(other: Any?): Boolean {
    return when {
    javaClass == other?.javaClass -> {
    other as Person
    name == other.name
    }
    else -> false
    }
    }
    }
    val first = Person("Me")
    val second = Person("You")
    println(first == second)

    I’m glad you’re back. The last few lesson were heavy, so in this one we’re going to slow down and integrate. We’ll discuss object equality and how to make object copies. While these are new, they are largely straightforward applications of things that we already know.

    Equality
    Equality

    What does it mean for two things to be the same? On one hand this is a deep epistemological question. But in our computer programs, it has practical importance.

    For example, I might have a list of items and want to remove all of the duplicates, or find something. I might require a key of some kind to access some information, and need to be able to tell whether the key you present is the same as the one that is required. These are examples of places in computer code where equality matters.

    Kotlin’s notion of equality is left up to us, the class designer, to define. Let’s look at an example of how.

    class Pet(val type: String)

    Note that, in Kotlin, using the == operator represents a call to the equals method on the first class passing the second as an argument:

    class First {
    override fun equals(other: Any?): Boolean {
    println("First.equals called")
    return false
    }
    }
    class Second {
    override fun equals(other: Any?): Boolean {
    println("Second.equals called")
    return false
    }
    }
    val first = First()
    val second = Second()
    println(first == second)
    println(second == first)

    Any.equals
    Any.equals

    equals is one of the methods defined by Any. That means, if we don’t override equals, our classes inherit the equals method defined by Any. This doesn’t do nothing, but it’s not particularly useful. Let’s see how it works:

    class Student(val name: String)

    Solve: Course Equality (Practice)

    Created By: Geoffrey Challen
    / Version: 2020.9.0

    Create a class named Course. Course should define a primary constructor that accepts three values: a String? (the department), a String? (the course number, and yes, this must be a String) and an Int (the enrollment). You are welcome to name these properties however you like, and they should be private. Your constructor should reject null departments and numbers and negative enrollments.

    Define a single method named equals. Your equals method accepts an Any? as a single parameter. It should return true if the passed object is a Course and has the same values for the department and number, and false otherwise.

    Copying Kotlin Objects
    Copying Kotlin Objects

    It may surprise you to learn that Kotlin has no built-in way to copy an object. The reasons for this are somewhat out of scope for us at this point. Put simply, objects may have complex internal state that is difficult or impossible to copy. Put another way, not every object may represent something that can be copied.

    However, when our objects can be copied we can enable this using a pattern called a copy constructor. Let’s look at how to do that together:

    class Student(val name: String)

    Solve: Restaurant Equality

    Created By: Geoffrey Challen
    / Version: 2021.9.0

    Create a class named Restaurant. Restaurant should define a primary single constructor that accepts three fields: a String? (the name), a String? (the cuisine) and an Int (the capacity). You are welcome to name these fields however you like, and they should be private. Your constructor should reject null names and cuisines and negative or zero capacities.

    You do not need to provide any getters and setters! Instead, define a single method named equals. Your equals method accepts an Any? as a single parameter. It should return true if the passed object is a Restaurant and has the same values for the name and cuisine, and false otherwise.

    CS People: James Mickens
    CS People: James Mickens

    James Mickens is a Harvard professor who does work on distributed systems and large-scale web services. He’s had papers published in the most prestigious venues in systems and networking, and received tenure at Harvard in 2019(1).

    James is also funny. Very funny! You can find his technology-driven humor in written form, but at least I think he’s at his best when giving a talk. Here’s one brief example, that also touches on an important issue:

    (And if you have more time, this longer talk is a personal favorite.)

    More Practice

    Need more practice? Head over to the practice page.