-4

Following code block when executed doesn't print the hashCodes and Boolean together. However when i do Boolean.toString(a==c) instead of a==c, it gets printed. Any reasons why this behavior

    String a = "Hello";
    String b = new String("Hello");
    String c =b;
    System.out.println(a.hashCode()+" "+b.hashCode()+" "+c.hashCode() + " " + a==c);
Yaman Jain
  • 1,254
  • 11
  • 16
  • 4
    That's because the precedence of == is lower than that of +. Try parentheses around a==c. – Professor Vector May 13 '17 at 11:11
  • Or, in short, you print `(a.hashCode()+" "+b.hashCode()+" "+c.hashCode() + " " + a) == c` – Turing85 May 13 '17 at 11:13
  • @ProfessorVector but then the hashCodes should be printed. Even they don't get printed – user3887166 May 13 '17 at 11:14
  • 1
    1. Include the orginal error message 2. Include what you expect 3. Include what happens instead 4. do your research before dumping questions onto SO, this is trivial if you have read +any* basic Java tutorial. – Polygnome May 13 '17 at 11:16
  • @user3887166 Why should they? `a + b + c + d == e` is still evaluated as `(a + b + c + d) == e`, no matter how many expressions you add. – glglgl May 13 '17 at 11:18
  • @glglgl Thanks for the info, i missed that badly. – user3887166 May 13 '17 at 11:24
  • [Java operator precedence guidelines.](http://stackoverflow.com/questions/2137690/java-operator-precedence-guidelines) There doesn't seem to be a good reference post on operator precedence yet. – Bernhard Barker May 13 '17 at 11:36
  • @user3887166 You didn't. Turing85 said the same 5 minutes before me. – glglgl May 13 '17 at 11:38

1 Answers1

-1

if you want to compare two strings use equals() method . The equals() function checks the actual contents of the string, the == operator checks whether the references to the objects are equal. Note that string constants are usually "interned" such that two constants with the same value can actually be compared with ==, but it's better not to rely on that.

if (strobj.equals(strobj2)) {
    ...
}

So if you want to print hashcodes along with the boolean result of comaparison of two strings ,do this

 System.out.println(a.hashCode()+" "+b.hashCode()+" "+c.hashCode() + " " + a.equals(c));
Vishesh
  • 308
  • 1
  • 9
  • 1
    This is not the actual problem at hand (no, I did not downvote). – Turing85 May 13 '17 at 11:14
  • i just want to differ between the == and equals() method(which is used for strings comparison),also what u r saying is also correct – Vishesh May 13 '17 at 11:16
  • Yeah, but you are not answering the question, though you have posted it as an answer. And you should use `equals(...)` for all objects in Java, not only for `String`s. – Turing85 May 13 '17 at 11:18
  • @Turing85 No. You should understand when to check for *identity* (`==`) and when to check for *equality* (`equals`), both have their use cases and blindly using one instead of the other isn't the solution, either. – Polygnome May 13 '17 at 13:19
  • @Polygnome of course, there are always exceptions =) but normally you want to check for equality. OP wanted specifically to check for identity, which is why this "answer" is no answer. – Turing85 May 13 '17 at 14:32