Is == checking is slower than equals() in Java? -
i understand == checks reference equality , equals checks value equality.
i want know if do
if(int_value == int_value)if(int_value.equals(int_value))
these notion of actual code; not actual code.
in java, 1 computed faster?
yesterday 1 of coding solution showed "time exceed" when used == , worked when switched equals. yes matters when using equality checking million times in code.
is == checking slower equals() in java?
comparing 2 object references of time faster doesn't make sense compare objects in way.
1) compare 2 int (primitive), == operator should favored :
int = ...; if(i == 1){ it natural , efficient way.
2) compare 2 integer(object), equals()way should favored:
integer = ... integer j = ... if(i.equals(j)){ you have not use == == compare identity of objects , work in range not in , depends on how integers instantiated.
so don't use == in case.
3) compare integer int, equals()way should avoided.
integer = ... if(i.equals(1)){ works should avoided boxes int 1 integer 1 before invoking equals().
, equals() method invokes intvalue() on passed integer argument equals(). in brief, performs checks , computations avoided.
you have 2 ways handle comparison case should have similar performance :
using
==comparison :integer = ... if (a==1){it unboxes
integerint, compares directlyintvalue==using
intvalue()of wrapper , comparing 2 primitiveints==** (we come first case) :integer = if(i.intvalue() == 1)){
generally, automatic unboxing performed jvm integer int invokes intvalue() .
Comments
Post a Comment