/* * Data classes in Java * Defined here are two classes one "data" and the other normal * They are otherwise identical. * The data class gets a useful toString method and an * equals method that compares the public fields of the class * as opposed to simple popinter equality of the standard class. * * @author ggtowell * created Aug 2, 2021 */ // A data class holding x and y values data class XY(val x:Int, val y:Int) // A normal class holding x and y values class CXY(x:Int, y:Int) fun main() { // create a bumch of instances val xy45 = XY(4,5) val xy55 = XY(5,5) val cxy45 = CXY(4,5) val xy45a = XY(4,5) val cxy45a = CXY(4,5) // print all of the instances println(String.format(" XY45 %s", xy45)) println(String.format(" XY55 %s", xy55)) println(String.format("CXY45 %s", cxy45)) println(String.format(" XY45a %s", xy45a)) println(String.format("CXY45a %s", cxy45a)) println("xy45==xy55 %s".format(xy45.equals(xy55))) println("xy45==xy45a %s".format(xy45.equals(xy45a))) println("cxy45==xy45 %s".format(cxy45.equals(xy45))) println("cxy45==cxy45a %s".format(cxy45.equals(cxy45a))) }