<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: PRATHMESH KUMBHAR</title>
    <description>The latest articles on DEV Community by PRATHMESH KUMBHAR (@prathmeshkumbhar04).</description>
    <link>https://dev.to/prathmeshkumbhar04</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4122076%2Fbe5a9a97-142f-4641-8a92-619a33149bf5.jpg</url>
      <title>DEV Community: PRATHMESH KUMBHAR</title>
      <link>https://dev.to/prathmeshkumbhar04</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/prathmeshkumbhar04"/>
    <language>en</language>
    <item>
      <title>Understanding == vs .equals() in Java: The Beginner's Trap That Cost Me Hours</title>
      <dc:creator>PRATHMESH KUMBHAR</dc:creator>
      <pubDate>Sat, 12 Sep 2026 18:47:16 +0000</pubDate>
      <link>https://dev.to/prathmeshkumbhar04/understanding-vs-equals-in-java-the-beginners-trap-that-cost-me-hours-5bmo</link>
      <guid>https://dev.to/prathmeshkumbhar04/understanding-vs-equals-in-java-the-beginners-trap-that-cost-me-hours-5bmo</guid>
      <description>&lt;p&gt;When I first started learning Java, I wrote a simple login system. The password entered by the user was exactly what was stored in the database. I looked at the screen, compared the two strings, and they were identical. &lt;/p&gt;

&lt;p&gt;Yet, my &lt;code&gt;if&lt;/code&gt; statement kept failing. &lt;/p&gt;

&lt;p&gt;I spent three hours staring at my screen, rewriting lines, and questioning my career choices, only to realize I fell into the ultimate Java beginner trap: &lt;strong&gt;using &lt;code&gt;==&lt;/code&gt; instead of &lt;code&gt;.equals()&lt;/code&gt;.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you are a fresher or a student learning Java, understanding this distinction early will save you days of frustration. Let’s break it down simply.&lt;/p&gt;




&lt;h2&gt;
  
  
  🧠 The Core Difference
&lt;/h2&gt;

&lt;p&gt;The easiest way to remember the difference is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;==&lt;/code&gt;&lt;/strong&gt; checks &lt;strong&gt;WHERE&lt;/strong&gt; the data is stored (Memory Location/Reference).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;.equals()&lt;/code&gt;&lt;/strong&gt; checks &lt;strong&gt;WHAT&lt;/strong&gt; the data actually is (Content/Value).&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🔍 1. The &lt;code&gt;==&lt;/code&gt; Operator (The Memory Address Check)
&lt;/h2&gt;

&lt;p&gt;When you use &lt;code&gt;==&lt;/code&gt; on objects (like Strings), Java checks if both variables point to the exact same spot in the computer's memory. &lt;/p&gt;

&lt;p&gt;Look at this example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;str1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Java"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;str2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Java"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

&lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;str1&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;str2&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// ❌ Prints FALSE&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Why is it false?&lt;/strong&gt; &lt;br&gt;
Because the &lt;code&gt;new&lt;/code&gt; keyword forces Java to create two completely separate objects in different memory locations. Even though they contain the exact same text, &lt;code&gt;==&lt;/code&gt; returns &lt;code&gt;false&lt;/code&gt; because their addresses are different.&lt;/p&gt;


&lt;h2&gt;
  
  
  📝 2. The &lt;code&gt;.equals()&lt;/code&gt; Method (The Value Check)
&lt;/h2&gt;

&lt;p&gt;If you want to compare the actual characters inside the string rather than their physical location in memory, you must use the &lt;code&gt;.equals()&lt;/code&gt; method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;str1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Java"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;str2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Java"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

&lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;str1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;equals&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;str2&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;//  Prints TRUE&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Why is it true?&lt;/strong&gt;&lt;br&gt;
Because &lt;code&gt;.equals()&lt;/code&gt; goes inside the object, compares it character-by-character (&lt;code&gt;J&lt;/code&gt;-&lt;code&gt;a&lt;/code&gt;-&lt;code&gt;v&lt;/code&gt;-&lt;code&gt;a&lt;/code&gt;), and confirms that the values are identical.&lt;/p&gt;




&lt;h2&gt;
  
  
  💡 Quick Summary Cheat-Sheet
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Operator/Method&lt;/th&gt;
&lt;th&gt;What does it compare?&lt;/th&gt;
&lt;th&gt;Best used for...&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;&lt;code&gt;==&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Memory Address (Reference)&lt;/td&gt;
&lt;td&gt;Primitive types (&lt;code&gt;int&lt;/code&gt;, &lt;code&gt;char&lt;/code&gt;, &lt;code&gt;boolean&lt;/code&gt;)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;&lt;code&gt;.equals()&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Actual Content (Value)&lt;/td&gt;
&lt;td&gt;Object types (&lt;code&gt;String&lt;/code&gt;, &lt;code&gt;ArrayList&lt;/code&gt;, Custom Objects)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  👋 Over to You!
&lt;/h2&gt;

&lt;p&gt;Falling into traps like this is a normal part of the learning journey when you are starting out. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is a silly bug or concept that kept you stuck for hours when you first started learning Java?&lt;/strong&gt; Let’s share our stories in the comments below to help other freshers feel less alone! 👇&lt;br&gt;
``&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>beginners</category>
      <category>java</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
