0

I am trying to understand some unexpected behavior in my Java code.

Here is the code:

TimeclockEntry t = db.getLastTimeclockEntryByEmployee(Integer.parseInt(employee));
String status = (t==null?"IN":t.getStatus());
if (operation != null)
    confirmation = getString(R.string.ttsJobIn);
else
    confirmation = getString(R.string.ttsClockIn);
if(t!=null) {
    if(status=="IN") {
        status = "OUT";
        in = "false";
        if (operation != null)
            confirmation = getString(R.string.ttsJobOut);
        else
            confirmation = getString(R.string.ttsClockOut);
    }
}
db.addTimeclockEntry(new TimeclockEntry(Integer.valueOf(employee), now, status));

The variable "status" holds the text representation of the user status. All I'm doing in this logic is swapping, for an auto-timeout function (so IN becomes OUT and vice-versa). Here I am checking the value of status against "IN", and when true, I want to do swap values.

However, when status is equal to "IN", the code does not execute (I am verifying with the Eclipse debugger, I watch the value of status, it's "IN", I have breakpoints set everywhere and it completely skips the inside of the if statement).

I can't understand what is happening. I am going to try to attach images of the debugging process to show what happens when status is "IN", how it skips the if clause entirely.

What could be happening? I originally had -- if(t!=null && status=="IN") but noticed this behavior, so I split up the if statement, no help either way.

status check, debugging value

This image shows the value of status at the time of the if statement. Breakpoints are set all around it, and the code inside the if statement should execute (changing the value of status from "IN" to "OUT").

https://i.stack.imgur.com/oVy9Z.png

status check, impossible value

This image shows the (seemingly) impossible value of status at the line that the debugger jumps to (instead of going inside the if statement, it jumps down past the if statement). In this case t is not null and status is equal to "IN", so the code inside the if statement should change the value of status to "OUT", but it is still set to "IN" at the breakpoint.

https://i.stack.imgur.com/PdDz3.png

dagey
  • 33
  • 5
  • 1
    ``==`` for strings in java doesn't do what you think it does ;) Use ``.equals()`` instead. – aruisdante Oct 13 '14 at 15:29
  • that took care of it, thanks. i understand that strings are objects as most things are in java, and i was using the '==' to avoid a null reference exception. i will just need to add checks for null first. – dagey Oct 13 '14 at 15:44

1 Answers1

3

Always use .equals when comparing Strings

if("IN".equals(status)) {

See if that helps :)

Pienterekaak
  • 456
  • 2
  • 7
  • 2
    You are right but just for providing some background to your answer: The reason the OP should use `equals` is that, in Java, Strings are stored as objects and the `==` operator compare the objects references not their values. `equals`, on the other hand, is the method provided to compare `String` objects values. – Pablo Francisco Pérez Hidalgo Oct 13 '14 at 15:34
  • this solution allows me to avoid checking status for null (which status.equals("IN") could throw). thanks. – dagey Oct 13 '14 at 15:47
  • well noticed :) thats why i put it in this example. – Pienterekaak Oct 13 '14 at 15:54