---
title: "Comment by Shai Almog on \"Why should we prefer using .equals() to == while comparing strings in Java?\" | Hashnode"
description: "Very nicely written! There&#39;s another trick that&#39;s guaranteed to work and would arguably be more performant: str.intern() == &quot;Interned String&quot;"
url: "https://hashnode.com/posts/why-should-we-prefer-using-equals-to-while-comparing-strings-in-java/61f022a587fd2728e7cd38e9/comment/61f0c3c4e247b2151e620181"
---

[Discussion](https://hashnode.com/posts/why-should-we-prefer-using-equals-to-while-comparing-strings-in-java/61f022a587fd2728e7cd38e9)

Comment [Why should we prefer using .equals() to == while comparing strings in Java?](https://hashnode.com/posts/why-should-we-prefer-using-equals-to-while-comparing-strings-in-java/61f022a587fd2728e7cd38e9)

[Shai Almog](https://hashnode.com/@talktotheduck)

[Jan 26, 2022](https://hashnode.com/posts/why-should-we-prefer-using-equals-to-while-comparing-strings-in-java/61f022a587fd2728e7cd38e9/comment/61f0c3c4e247b2151e620181)

Very nicely written!

There's another trick that's guaranteed to work and would arguably be more performant:

```
str.intern() == "Interned String"
```

The reason `==` sometimes works is the interned string pool inside String. The `intern()` method makes sure the string is properly stored there and returns the interned instance. This way you can use pointer arithmetic which is faster. Notice that hardcoded strings are always interned so you can rely on that.

The one big downside of this is that most developers and tools looking at `==` code will assume it's a bug.

[Samadrita Shaw](https://hashnode.com/@shawsamadrita)

[Jan 26, 2022](https://hashnode.com/posts/why-should-we-prefer-using-equals-to-while-comparing-strings-in-java/61f022a587fd2728e7cd38e9/comment/61f0c3c4e247b2151e620181#reply-61f0decc87fd2728e7cd4ba8)

Thank you so much for sharing this! This is something new for me. 🙌
