2

I discover during debbuging my android app the strange behaviour.

There is expression:

if (r == true)

where var r has the value true but the whole statement is false. I try to use object Boolean and also primitive type boolean.

I'm sure that I make some basic mistake.

Here is the screen from debbuger.

enter image description here

Edit:

I'm using java.lang.Boolean.

method isSyncRequired returns Boolean.TRUE and it is compared in if-else block.

if(isSyncRequired(s))
if (r)
if (r == true)

dont't work.

misco
  • 1,912
  • 4
  • 41
  • 68
  • I forgot... I also try the statement `if(r)`. – misco Feb 19 '13 at 22:02
  • Out of curiousity, why aren't you just doing: 'if(isSyncRequired(s))' or even 'if(r)' you're now comparing a boolean to see if it's true while you could just use the value itself. And when using Boolean use equals not == – Simon Verhoeven Feb 19 '13 at 22:03
  • I firstly use `if(isSyncRequired(s))` but app has got the strange behaviour. Then I start to find the bug. I change it on `if(r)` later on `if(r == true)` etc. – misco Feb 19 '13 at 22:08
  • Try cleaning your project. You may have source code out-of-synch with the compiled classes. – GriffeyDog Feb 19 '13 at 22:17
  • @GriffeyDog without the success – misco Feb 19 '13 at 22:23
  • @misco: may I suggest forgetting the debugger and going for a simple `System.out.println("r = " + r);` to verify that return value? – thkala Feb 19 '13 at 22:28
  • Without debugger it prints true and the comparison is the correct. But why does it happen? – misco Feb 19 '13 at 22:36
  • I rewrited my code. I kept the same return types and values. I removed one method and it seems to work properly. But I don't understand to this issue. – misco Feb 19 '13 at 22:49
  • possible duplicate of [Why does if(Boolean.TRUE) {...} and if(true) {...} work differently in Java](http://stackoverflow.com/questions/28546321/why-does-ifboolean-true-and-iftrue-work-differently-in-java) – Raedwald Feb 21 '15 at 08:57
  • @Raedwald Isn't it the other way around? The linked question is a duplicate of this one. – Tom Feb 21 '15 at 09:20

3 Answers3

11

Because you're using a capital-B Boolean, which is an object, so == uses reference equality semantics.

What you should be doing is just

if (r)

There's no need to test if it's equal to true. Or inline it:

if (isSyncRequired(s)) {
Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
  • 2
    Your rationale does not appear to agree with [the JLS section on boolean equality](http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.21.2). According to the JLS, `r` should be unboxed before the comparison. Perhaps this is something Android-specific? – thkala Feb 19 '13 at 22:10
  • I distinguish the difference between Object and primitive. But your suggestion dont't work. – misco Feb 19 '13 at 22:11
  • I would agree, but the OP's issue appears to unambiguously indicate that's not working out in their scenario. – Louis Wasserman Feb 19 '13 at 22:11
  • 1
    @misco: Give more details, if that doesn't work. Are you e.g. sure you're using `java.lang.Boolean`? – Louis Wasserman Feb 19 '13 at 22:11
  • @thkala Android? As I understand it Android development uses Java source code, a compiler to generate class files and then a transcoder to create the Dalvik files. All the auto[ub]boxing "fiction" occurs in the Java compiler, so there shouldn't be anything left for the Android piece to mess up. (As usual, see output from `javap -c`.) – Tom Hawtin - tackline Feb 19 '13 at 22:13
  • @TomHawtin-tackline: true. I am out of ideas, except for Louis' suggestion about the OP not using `java.lang.Boolean` or some weird debugger bug (pun not intended). – thkala Feb 19 '13 at 22:16
  • As @thkala already mentioned, it doesn't matter if you use `Boolean` or `boolean`, the language spec clearly states that a) `==` and `!=` comparisons with those types do _not_ use reference equality and b) even if that was the case, the [spec on boxing conversion](http://docs.oracle.com/javase/specs/jls/se5.0/html/conversions.html#5.1.7) (its the same for SE7) states that for `boolean`/`Boolean` the resulting objects are guaranteed to be identical. So please remove the first line or your answer, its plain wrong. – Simon Lehmann Feb 20 '13 at 10:57
  • @SimonLehmann: Not without a better explanation as to the OP's problem. – Louis Wasserman Feb 20 '13 at 18:00
  • What explanation could possibly change the language specification? The statement is wrong and will be wrong no matter what anybody explains or discusses. – Simon Lehmann Feb 20 '13 at 19:11
1

== compares by reference. You are comparing a Boolean object to a boolean value.

Use if (r) instead.

John Leehey
  • 22,052
  • 8
  • 61
  • 88
1

You should unbox the Boolean value. Try

if (r.booleanValue())

or

if (r.booleanValue() == true)
danijel
  • 576
  • 4
  • 10