[JAVA] Now in the third year, the misunderstanding that I noticed is the difference between the equals method and ==

** I misunderstood. </ font> **

From now on, about 3 years ago ...

When I participated in a development project in JAVA with empty knowledge that I didn't even know the language.

While learning from JAVA textbooks, secco-seco, coding in development projects ...

Pull request ... No good ... Textbook ... Correction ... Pull request ... No good ... Correction ... google ... No good ...

I worked in such a state.

At that time, a word from my senior ...

It's probably left in my mind all the time.

** Use the equals method to compare Strings. </ font> **

A word from my senior is correct.

However, Japanese is difficult ...

With just a little bit of thinking, it seems to be bad or good.

Unexpectedly, I remembered it like this ...

** "Use equals method to compare Strings." == "Use equals method only when comparing Strings." </ Font> **

A few months later, I studied for the Oracle-certified JAVA exam and got my qualification.

But even so, the old memory wasn't rewritten.

In the JAVA qualification reference book, I wrote in a memo that "the wrapper class is an equals method", so I noticed that I was misunderstanding while studying.

Nevertheless ...

Then I wondered why it was okay to remain misunderstood.

What I noticed there was that there were extremely few opportunities to "calculate numerical values" in past projects.

This time, I noticed this misunderstanding when I went to help with system development to manage a certain point.

There, int changed to Integer, Integer changed to int, and there was a boxing storm.

So, the numbers are extremely severe.

There, I noticed.

** "Because it's a String, it's not an equals method!" </ Font> **

When··

However, the reason why this mistake was clearly engraved in my mind now is probably because I have experienced UT many times.

-If the instance changes, even if the values of the contents are the same, there will be a mismatch when comparing the objects.

I remember when I was worried that it wouldn't work when comparing objects with each other when comparing expected and result values using Assert.

The embarrassing story has been lengthened, but the conclusion is that:

boolean result = false;
int primitiveA = 10;
int primitiveB = 10;

//The result is true
result = primitiveA == primitiveB;

Integer integerA = new Integer(10);
Integer integerB = new Integer(10);

//The result is false
result = integerA == integerB;
//The result is true
result = integerA.equals(integerB);

Occasionally, it may be necessary to go back to the beginning and review the basics.

Recommended Posts