Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

Clarification This question is not about comparing two String object variables, it is about comparing a String object variable to a String literal. I realize that the following code is useful and would produce different results than .equals():

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

In this case a and b might actually point to the same area in memory, even if it's not because of interning. In the first example though, the only reason it would be true is if Java is doing something behind the scenes which is not useful to me, and which I can't use to my advantage.

Follow up question Even if a and the string-literal both point to the same area in memory, how is that useful for me in an expression like the one above. If that expression returns true, there isn't really anything useful I could do with that knowledge, is there? If I was comparing two variables, then yes, that info would be useful, but with a variable and a literal it's kinda pointless.

like image 661
Marius Avatar asked May 14 '10 18:05

Marius


People also ask

Why == does not work in Java?

The == operator does not work reliably with strings. Use == to compare primitive values such as int and char. Unfortunately, it's easy to accidentally use == to compare strings, but it will not work reliably. Remember: use equals() to compare strings.

Can you == strings in Java?

To compare these strings in Java, we need to use the equals() method of the string. You should not use == (equality operator) to compare these strings because they compare the reference of the string, i.e. whether they are the same object or not.

What does unclosed string literal mean in Java?

As its name implies, the unclosed string literal error refers to a string literal which has not been closed. More specifically, this means that the Java compiler has failed to interpret a string literal due to being unable to locate the double quote expected to close i.e., mark the end of it.


1 Answers

Actually they can indeed be the same reference if Java chooses to intern the string. String interning is the notion of having only one value for a distinct string at runtime.

  • http://en.wikipedia.org/wiki/String_intern_pool

Java notes about string interning

  • http://javatechniques.com/blog/string-equality-and-interning/
like image 170
JaredPar Avatar answered Oct 23 '22 21:10

JaredPar