Info |
---|
In Java, there are two types of object comparison operators:
|
...
|
Example:
Code Block | ||
---|---|---|
| ||
package EqualsMethod; public class EqualOperatorDemo { public static void main(String[] args) { String str1 = "Hello"; String str2 = "Hello"; String str3 = new String("Hello"); System.out.println(str1 == str2); // true (Both references point to the same "Hello" object in the string pool) System.out.println(str1 == str3); // false (str1 points to a different object than str3) } } |
...
In the example above, str1
and str2
both reference the same object in the string pool, so str1 == str2
returns true
. However, str1
and str3
reference different objects, even though they have the same value, so str1 == str3
returns false
.
Info |
---|
|
...
|
Panel | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
The Let's use an everyday example: Suppose you have two cups. At a glance, they might look similar, but to check if they are truly identical, you would compare their features — like color, size, shape, and design. If all these features match, then you can conclude that the two cups are equal. In Java, objects are like these cups. The However, you can override the So, the |
Example:
Code Block | ||
---|---|---|
| ||
package EqualsMethod; public class EqualOperatorDemoTwo { public static void main(String[] args) { String str1 = "Hello"; String str2 = "Hello"; String str3 = new String("Hello"); System.out.println(str1.equals(str2)); // true (The content of the strings is the same) System.out.println(str1.equals(str3)); // true (The content of the strings is the same) } } |
...