The if would be here
# announcements
h
The if would be here
d
Have you tried
if (!object.string.equals("string")) { code }
?
k
How do you know the strings actually match?
☝️ 1
h
@dewildte I hadn't, I just tried now and it appears .equals() is behaving as expected. I honestly thought == and .equals would work the same for String. Thanks for pointing me towards a solution.
k
They are the same!
(in Kotlin at least, not in Java)
h
@karelpeeters I put a few breakpoints in and manually evaluated
@karelpeeters haha. dun dun dun...plot thickens
(No idea why one works and not the other)
k
Can you take a look at the generated bytecode to see what
==
compiles down in your code?
How did you test equality with the debugger?
Are you sure you didn't accidentally do
===
?
d
From my (limited) understanding
!=
and
!string.equals(...)
are not the same.
h
@karelpeeters I'm not sure how to examine the bytecode, is it something I can easily see?
k
Tools > Kotlin > Show generated bytecode
(and then press decompile to get the Java version of the code)
d
CTRL+A "show kotlin byte code"
k
@dewildte Well except for some nullability stuff
!=
and
!.equals
are exactly the same.
d
Are they structural or referential?
h
To debug in AS(this is a android project) I put a break point in AFTER object is instantiated and also on the same line as the if statement. If you hover over the var in AS while at a stop point you can look at the underlying data
d
===
is referential is it not?
h
So I would know the value of object.string before the if code ran
k
Yes, K
===
is reference equality, so
==
, and K
==
is structural so
.equals
.
d
Java will give you a warning when trying to compare strings with
==
and
!=
. Warning says to use
.equals(...)
That's why I suggested using it.
h
That show bytecode feature is cool. but I don't know how to read bytecode lol
Give me a sec to look
k
That's what the compile button is for simple smile
@dewildte But this is Kotlin!
d
Wibbly wobbly Kotlin javaey stuff.
lol
Anyway
.equals(...)
works for John.
I have contributed my piece. lol
h
Seems they call different things in bytecode?
d
AHA!
h
The .equals:
INVOKEVIRTUAL java/lang/String.equals (Ljava/lang/Object;)Z
d
The truth is revealed!
h
The ==: INVOKESTATIC kotlin/jvm/internal/Intrinsics.areEqual (Ljava/lang/Object;Ljava/lang/Object;)Z
Or am I reading things wrong haha
k
And you'll never guess what the source of that function is like...
h
lol
k
Copy code
public static boolean areEqual(Object first, Object second) {
    return first == null ? second == null : first.equals(second);
}
h
haha
d
Wow
k
So there must be something else going on.
d
That is casting them as Object, but that should not matter.
I swear string equality is tricky.
h
I'm far from a "real" programmer, but nothing jumps out at me as to why this is happening.
k
Would it be possible to create a minimal example we can run?
d
Dude you just compiled byte code. How much more "real programmer" do you need?
h
A job
😉
d
Lol!!!!
h
Let me see how much code is related to this
d
I am laughing so hard right now
h
👍
I think I can get an example made up in a couple mins, will post if I can recreate
d
Cool
h
Yea, of course... I can't recreate in a separate project. Figures...
k
It could be a debugger bug as well.
h
Yea, I can't reproduce. Figures! haha. Thanks for the help!
👍 2