0

I have the strangest case which challenges my understanding about the fundamental way Java works.

I have the following code which I step through using the debugger to the "return result == "OK" line, where I expect the execution for this method to finish. But it doesn't! If I continue stepping with the debugger, I see that it steps to the very last line (without throwing an exception) and returns false!

Can anyone shed any light on this?

protected Boolean doInBackground(Void... params) {

    try
    {
        String json = buildJson();
        String encryptedData = encrypt(json);
        String base64Data = Base64.encode(encryptedData.getBytes(UTF8));
        String url = webserviceUrl + "?data=" + base64Data;
        String result = readUrl(url);

        return (result == "OK");  // Debugger gets to here but doesn't return!
    }
    catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return false;  // Debugger gets to here instead, and returns false!
}
Flyingkiwi
  • 3,066
  • 3
  • 15
  • 17
  • 2
    it's just an impression of the debugger/ide you are using. If no exceptions are thrown the first return is hit. Btw you should use equals to compare strings – Blackbelt Mar 22 '15 at 14:16
  • 1
    You can't test String equality with the "==" operator, use **result.equals("OK")** instead – SamTebbs33 Mar 22 '15 at 14:18
  • Thanks Blackbelt, but I can see that the result DOES EQUAL "OK", but it goes to the next statement anyway. You are right about using equals though (I am a C# programmer by day, and I sometimes forget). I changed == to .equals, and everything started working. – Flyingkiwi Mar 22 '15 at 14:19

0 Answers0