Why doesn't Java warn about a == "something"?

Posted by Marius on Stack Overflow See other posts from Stack Overflow or by Marius
Published on 2010-05-14T18:44:22Z Indexed on 2010/05/14 18:54 UTC
Read the original article Hit count: 200

Filed under:
|

This might sound stupid, but why doesn't the Java compiler warn about the expression in the following if statement:

String a = "something";
if(a == "something"){
  System.out.println("a is equal to something");
}else{
  System.out.println("a is not equal to something");
}

I realize why the expression is untrue, but AFAIK, a can never be equal to the String literal "something". The compiler should realize this and at least warn me that I'm an idiot who is coding way to late at night.

© Stack Overflow or respective owner

Related posts about java

Related posts about language-design