-3

I have two string in Java:

String a = "ab";
String b = "ab";

And I test them with the string.equals() method because the == operator only checks whether the references to the objects are equal.

Why can I use == in JavaScript?

hakre
  • 193,403
  • 52
  • 435
  • 836
Tania Marinova
  • 1,788
  • 8
  • 39
  • 67
  • 14
    Java and JavaScript are different languages. – Denys Séguret May 22 '13 at 07:55
  • 4
    Are you asking why this two different languages have a different design ? – Denys Séguret May 22 '13 at 07:56
  • 3
    in fact, javascript was originally called livescript and had nothing at all to do with java – tay10r May 22 '13 at 07:57
  • I don't see the need for closing this even though it has a simple answer. It's a valid question which most of us had at one point in time, and a decent answer could really be a light-bulb moment for a lot of people. – Supr May 22 '13 at 08:03
  • 2
    Constant string literals are interned in Java. a == b, here would return true – c.P.u1 May 22 '13 at 08:05
  • 1
    @Supr language design comparison questions don't belong there. What kind of answer do you expect? This question will solicit speculation. Even if you got someone who designed _both_ languages (or combine two authors' questions), how useful is that going to be? – John Dvorak May 22 '13 at 08:15
  • @JanDvorak I didn't read it as a question about why they were designed differently, but rather about the technical difference. Not `why did the authors define == as XXX` but `why does == work at all in JavaScript` -- `what is == doing if it's not a reference check? Is == overloaded in JS? Are its arguments converted somehow?Does it have something to do with string interning?`. I would expect an answer that goes into those issues and explains what's going on and what's *not* going on. OP is IMO asking very specifically how `==` works in JavaScript and not for a comparison. Java is just backdrop – Supr May 22 '13 at 08:48
  • @Supr if that is what the asker meant, then it should be edited to make this more obvious. More specifically, the title should be change so that the question doesn't appear to question why either language is designed such way, but rather _what_ is the design. – John Dvorak May 22 '13 at 08:55
  • @JanDvorak I don't see how he's questioning anything. He's just asking why can he use `==` which to me is the same as asking how does it work, or in your terms: what is the design? But let's just agree to interpret it differently ;) – Supr May 22 '13 at 09:17
  • @Supr well, you can try to reopen, but I suggest editing first ;-) – John Dvorak May 22 '13 at 09:21
  • @JanDvorak nah, I have better things to do than to babysit questions. Looks like the given answers helped OP already anyway. – Supr May 22 '13 at 09:26

3 Answers3

1

In Java, you should use String.equals()

== Operator in Java is detecting if they are the same object on both side since String in Java is a Object but not primitive like int, float. == Will check if they have same address

Exmaple:

String a = new String("hello");
String b = "hello";

a.equals(b) // true
a == b // false

a and b are not the same object, But they have same content.

boisvert
  • 3,679
  • 2
  • 27
  • 53
Jeff Lee
  • 783
  • 9
  • 17
1

In Java == compares whether the variables are pointing to the same object. It does not do a semantic comparison. In Javascript the == compares the strings.

Steve
  • 7,171
  • 2
  • 30
  • 52
0

String a = "ab"; is assigning the value.

== in Java checks there reference of the strings to each other. .equals() checks the values.

You should look at this question for more answers on .equals vs == in java

Also for completeness, I should mention in javascript you can also do === so things like null equal to false.

true === 1 //false

true == 1 //true

Community
  • 1
  • 1
David
  • 19,577
  • 28
  • 108
  • 128