noi-techpark / it.bz.beacon.api

This repository contains the sourcecode of the administration web-app that allows to manage the beacon network installed withing the Beacon Suedtirol project.
Other
1 stars 3 forks source link

Comparison of Objects without equals() #75

Closed Piiit closed 3 years ago

Piiit commented 3 years ago

https://github.com/noi-techpark/it.bz.beacon.api/blob/10ecc759d8951816cd68716c15db5e15c6dca8f1/src/main/java/it/bz/beacon/api/service/beacon/BeaconService.java#L130

Hi @noctho I think at this point we should better use equals()... otherwise we are comparing Long objects with ==, and thus against their addresses.

noctho commented 3 years ago

I'm not sure about it, because I think it is possible that it unpacks the wrapper classes before comparison. But the better way is definitely to use equals(). Fixed, see #76

Piiit commented 3 years ago

@noctho This will not work for values greater than 127...

You can test it if you like

public class Test {
    public static void main(String[] args) {
        Long l1 = Long.valueOf(10000);
        Long l2 = Long.valueOf(10000);
        System.out.println(l1 == l2);
        Long l3 = Long.valueOf(1);
        Long l4 = Long.valueOf(1);
        System.out.println(l3 == l4);
    }
}

...this gives:

false
true

See https://stackoverflow.com/questions/20541636/comparing-boxed-long-values-127-and-128