Java: What is the difference between equals and ==

This is one of those things that can stump newbies and veterans

This is going to be a fun and personal post for me. The problem that we are going to look at in this article is something we are taught as young software developers. 

Java Equals vs '=='

I saw this question on a facebook group that I am a part of and I made the same mistake at work debugging a critical ticket. If you want to find out what trips up newbies and veterans alike, continue reading this article. 

The Problem

The problem we are going to look at today has to do with comparing values in Java. This is language agnostic issue though as every language has its own way to deal with object values and references. 

We are going to start by looking at a question I saw in the facebook group I am in. Given the title and subject of this article, you might be able to easily find the problem. What is the output of this code and why is that output returned to us? 

If you run this code and enter “C” as your answer, the program will print out “You are incorrect”. 

Java Equals Example

So what happened here? Why didn’t the program print out “You are correct”? The problem with this program is with the following code. 

The variable answer is a String and we are using == to compare the value to “C”. We know that if we compare the value “C” to “C” that they should be equal. In fact, they are equal but the problem lies with the operator. 

The “==” Operator

In Java, we use the “==” operator to compare 2 objects to see if the refer to the same place in memory. When we create the variable answer and assign it a value that is a reference to that object. When we compare that object in memory to the value “C” those are not the same so “==” will return false. The following is an example of where 2 variables reference the same object. 

The Solution

If that is the case how can we fix our program? The String class actually contains a method called .equals. With that knowledge, we can fix our application by replacing that “==” with .equals. 

Now instead of comparing objects, we are simply comparing values. That is why you will often hear programmers say “compare by value, not reference”. We probably fix a couple other issues with this program but that is neither here nor there. 


Remember when I told you that I made this same mistake at work? There is actually a good reason for this. I was working in a Java program and this gives me some problems from time to time because of my love for the Groovy Programming language. I would say I am writing Groovy 90% of the time and because of that, I become used to the syntax. In Groovy we actually overload the “==” operator so that comparison would actually work. 

Question: What are some of the fundamental programming concepts that still trip you up once in awhile? 

Hi, I’m Dan Vega. I am a Software Engineer living just outside of one of my favorite places in the world, Cleveland, Ohio. I am a self-taught programmer who firmly believes that you can accomplish anything in this life if you put your mind to it, roll up your sleeves and are willing to put in the work.

I have been writing software for over 18+ years now and I use my knowledge, passion, and influence to help other developers achieve their goals. I teach online and currently, have over 30,000 students.

When I am not writing software, I have a number of things that really keep me busy. I love to read books, listen to podcasts and watch online courses. I have a real thirst for knowledge and it consumes me every single day of the week. I also love to run & lift heavy weights which keep me balanced throughout the week.

Please note: I reserve the right to delete comments that are offensive or off-topic.