Is == checking is slower than equals() in Java? -


i understand == checks reference equality , equals checks value equality.

i want know if do

  1. if(int_value == int_value)

  2. 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 integer int , compares directly int value ==

  • using intvalue() of wrapper , comparing 2 primitive ints ==** (we come first case) :

    integer = if(i.intvalue() == 1)){

generally, automatic unboxing performed jvm integer int invokes intvalue() .


Comments

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -