Why do we need hashCode, equals ? In most if the c...
# announcements
s
Why do we need hashCode, equals ? In most if the cases we never use it.
s
useful when you need to compare objects
s
@suhas That’s okay. So most of the case we never use hashCode and equals. So choosing
data class
over
class
will be overhead right ? class also generates setters and getters
n
but data class generates a toString, which might be useful unless you want your toString to be json output
s
@Nikky I don’t think
Gson
uses that
toString
from
data class
. Only use of
toString
is logging
n
exactly
sometimes you want to look at your data, then having it readable instead of the default memory addresses is .. helpful
s
@Nikky But that will be only helpful during debugging
One thing I found that
data class
is taking thrice size of normal
class
. Another problem is
proguard
is not removing unused hashcode and equals.
c
you can't really know if those are never used. your app is very likely to contain at least one place where Object.equals() or Object.hashCode() is called. statically checking whether that code could be dealing with an instance of your data class at runtime is very expensive to impossible
s
@cketti I got the thing regarding proguard. But then it will be recommended to use
class
as model as compared to
data class
p
Did you measure if the overhead actually affects you? I mean if you want no overhead for the sake of no overhead, follow the tutorial on writing your app in assembly which recently popped up on Reddit ;-)
5
s
@Paul Woitaschek I have already mentioned about unnecessary size of class
h
hashCode is used for storing objects in HashMap and pretty much every Collection starting with Hash.