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.