Mark Ingram
Mark Ingram

Reputation: 73605

Strange program flow

I'm really puzzled by the following piece of code:

// Get the content text
String contentText = null;
Header contentEncodingHeader = m_httpEntity.getContentEncoding();
final String contentEncodingValue = contentEncodingHeader != null ? contentEncodingHeader.getValue() : ""; // In my example, this is set to "gzip"

if (contentEncodingValue == "")
{
    contentText = this.GetResponseContentText(inputStream, charset);
}
else if (contentEncodingValue == "gzip")
{
    contentText = this.GetResponseContentText_GZip(inputStream, charset);           
}

return contentText;

When I step over the lines of code, it executes in the following order:

1) if (contentEncodingValue == "")
{
3)  contentText = this.GetResponseContentText(inputStream, charset);
}
2) else if (contentEncodingValue == "gzip")
{
    contentText = this.GetResponseContentText_GZip(inputStream, charset);           
}

4) return contentText;

And even stranger still, is it doesn't even enter the GetResponseContentText function. I really am confused. Can anyone shed any light on this?

Also, if I comment out the if statement, it works fine (goes into the GetResponseContentText_GZip function).

Upvotes: 0

Views: 84

Answers (1)

ccheneson
ccheneson

Reputation: 49410

From string comparison, you would want to use equals instead of ==

if (contentEncodingValue.equals("")) {
...
}
else if (contentEncodingValue.equals("gzip")) {
...
}

Upvotes: 2

Related Questions