Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What use does the == operator have for String?

Tags:

java

string

In Java, if one is to check if two Strings are equal, in the sense that their values are the same, he/she needs to use the equals method. E.g. :

String foo = "foo"; 
String bar = "bar";
if(foo.equals(bar)) { /* do stuff */ }

And if one wants to check for reference equality he needs to use the == operator on the two strings.

if( foo == bar ) { /* do stuff */ }

So my question is does the == operator have it's use for the String class ? Why would one want to compare String references ?

Edit: What I am not asking : How to compare strings ? How does the == work ? How does the equals method work?

What I am asking is what uses does the == operator have for String class in Java ? What is the justification of not overloading it, so that it does a deep comparison ?

like image 845
Evdzhan Mustafa Avatar asked Feb 27 '15 22:02

Evdzhan Mustafa


People also ask

Can we use == operator for strings?

Using the == operator compares the object reference. Using the equals() method compares the value of the String . The same rule will be applied to all objects. When using the new operator, a new String will be created in the String pool even if there is a String with the same value.

What happens when you use == with strings?

The == operator == is an operator that returns true if the contents being compared refer to the same memory or false if they don't. If two strings compared with == refer to the same string memory, the return value is true; if not, it is false.

Can we use == for string comparison?

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. On the other hand, equals() method compares whether the value of the strings is equal, and not the object itself.

How does the == operator work?

The equal-to operator ( == ) returns true if both operands have the same value; otherwise, it returns false . The not-equal-to operator ( != ) returns true if the operands don't have the same value; otherwise, it returns false .

What is == used for in Java?

Both equals() method and the == operator are used to compare two objects in Java. == is an operator and equals() is method. But == operator compares reference or memory location of objects in a heap, whether they point to the same location or not.

When using the == operator with strings What is Java comparing?

The Java equals() method compares two string objects, the equality operator == compares two strings, and the compareTo() method returns the number difference between two strings. String comparison is a crucial part of working with strings in Java.


2 Answers

Imagine a thread-safe Queue<String> acting as a communication channel between a producer thread and a consumer thread. It seems perfectly reasonable to use a special String to indicate termination.

// Deliberate use of `new` to make sure JVM does not re-use a cached "EOT".
private static final String EOT = new String("EOT");
...
// Signal we're done.
queue.put(EOT);


// Meanwhile at the consumer end of the queue.
String got = queue.get();
if ( got == EOT ) {
  // Tidy shutdown
}

note that this would be resilient to:

queue.put("EOT");

because "EOT" != EOT even though "EOT".equals(EOT) would be true.

like image 53
OldCurmudgeon Avatar answered Oct 20 '22 00:10

OldCurmudgeon


What use is there for it? Not much in normal practice but you can always write a class that operates on intern()-ed strings, which can then use == to compare them.

Why it isn't overloaded is a simpler question: because there is no operator overloading in Java. (To mess things up a bit, the + operator IS sort of overloaded for strings, which was done to make string operations slightly less cumbersome. But you can argue that's just syntactic sugar and there certainly is no operator overloading in Java on the bytecode level.)

The lack of an overloaded == operator made the use of the operator much less ambiguous, at least for reference types. (That is, until the point autoboxing/unboxing was introduced, which muddies the waters again, but that's another story.) It also allows you to have classes like IdentityHashMap that will behave the same way for every object you put into it.

Having said all that, the decision to avoid operator overloading (where possible) was a fairly arbitrary design choice.

like image 41
biziclop Avatar answered Oct 20 '22 01:10

biziclop