---
title: "Discussion on \"Why should we prefer using .equals() to == while comparing strings in Java?\" | Hashnode"
description: "Discussion on \"Why should we prefer using .equals() to == while comparing strings in Java?\". There is often a confusion between == and .equals(). Let's clear that confusion in a very simple way!"
url: "https://hashnode.com/posts/why-should-we-prefer-using-equals-to-while-comparing-strings-in-java/61f022a587fd2728e7cd38e9"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [Why should we prefer using .equals() to == while comparing strings in Java?](https://samadritashaw.hashnode.dev/why-should-we-prefer-using-equals-to-while-comparing-strings-in-java?utm_source=hashnode&utm_medium=feed)

There is often a confusion between == and .equals(). Let's clear that confusion in a very simple way!

Jan 25, 2022 · 4 min read · samadritashaw.hashnode.dev  
Updated Feb 8, 2022  
Read the article

-   [#java](https://hashnode.com/tag/java)
-   [#beginners](https://hashnode.com/tag/beginners)
-   [#string](https://hashnode.com/tag/string)
-   [#data-structures](https://hashnode.com/tag/data-structures)
-   [#blogging](https://hashnode.com/tag/blogging)

## Written by

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

@shawsamadrita · Jamshedpur, India

  
[View profile](https://hashnode.com/@shawsamadrita)

The more I share, the more I learn.

I am junior, pursuing bachelors in Computer Science and Engineering. Currently I am strengthening my DSA in Java and have started learning HTML and CSS to move forward in the path of web development.

## Responses 3

Markdown supported · ⌘↵ to post

[Sayantani Dey](https://hashnode.com/@sayantani-dey)

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

The explanation was so clear, looking forward to your blogs❤️

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

[Feb 12, 2022](https://hashnode.com/posts/why-should-we-prefer-using-equals-to-while-comparing-strings-in-java/61f022a587fd2728e7cd38e9/comment/6207d2d2a2be760e35396220#reply-6207df52a2be760e3539636c)

Thanks a bunch, Sayantani!

[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. 🙌

[Zafeer Rangoonwala](https://hashnode.com/@Zafeer-R)

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

When working with string builder .equals() method apparently doesn't work. In order to make it work you first need to convert the string builder to string by using `.toString()` and then use `.equals()`.

Eg:

`sb1.toString().equals(sb2.toString());`

Everything else is just perfect and I felt like adding this piece of information. 😊

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

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

Yes, you're right. Actually this blog post only includes about how == differs from .equals() and that's why I haven't included anything about Stringbuilder as that is a bit different concept.

Thank you for adding this information, this might help someone! 😄
