ITP

BSc IT - Structure Programming Methodology - Java

Difference between equals() method and equality operator (==) in java ?

In : BSc IT Subject : Structure Programming Methodology - Java

In Java, the == operator  and the equals() method  are both used to compare objects, but they work differently. The == operator checks if two references point to the same object in memory , meaning it compares the memory addresses . On the other hand, the equals() method checks if the actual contents or values  of the objects are the same. For example, with strings, == tells you if two string variables refer to the exact same object, while equals() tells you if the text inside the strings is the same. So, use == for checking if variables point to the same object, and equals() to check if their values are equal.

About us

A truly open platform where you may ask questions and get answers. We also provide comprehensive and easy-to-understand answers to question papers.  discover...

Site status

Flag Counter

Privacy Policy

Sitemap