filmov
tv
JAVA : What is the difference between == and equals() in Java with Example?
Показать описание
JAVA : What is the difference between == and equals() in Java with Example?
SDET Automation Testing Interview Questions & Answers
We will be covering a wide range of topics including QA manual testing, automation testing, Selenium, Java, Jenkins, Cucumber, Maven, and various testing frameworks.
JAVA : What is the difference between == and equals() in Java with Example?
In Java, the == operator and the equals() method are used for comparing two objects, but they have different meanings and usages.
The == operator is used to compare the object references of two objects to see if they are pointing to the same memory location. It checks if two objects are identical or not. When two objects are compared using ==, it returns true if both objects are pointing to the same memory location, and false otherwise.
For example:
String str1 = "Hello";
String str2 = "Hello";
String str3 = new String("Hello");
In the above example, str1 and str2 are string literals, and they are pointing to the same memory location, so str1 == str2 returns true. However, str3 is created using the new keyword, and it is pointing to a different memory location, so str1 == str3 returns false.
On the other hand, the equals() method is used to compare the contents of two objects to see if they are equal or not. It checks if two objects are equivalent or not. When two objects are compared using equals(), it returns true if the contents of both objects are the same, and false otherwise.
For example:
String str1 = "Hello";
String str2 = "Hello";
String str3 = new String("Hello");
In summary, the == operator compares the object references of two objects to see if they are pointing to the same memory location, while the equals() method compares the contents of two objects to see if they are equal or not.
SDET Automation Testing Interview Questions & Answers
We will be covering a wide range of topics including QA manual testing, automation testing, Selenium, Java, Jenkins, Cucumber, Maven, and various testing frameworks.
JAVA : What is the difference between == and equals() in Java with Example?
In Java, the == operator and the equals() method are used for comparing two objects, but they have different meanings and usages.
The == operator is used to compare the object references of two objects to see if they are pointing to the same memory location. It checks if two objects are identical or not. When two objects are compared using ==, it returns true if both objects are pointing to the same memory location, and false otherwise.
For example:
String str1 = "Hello";
String str2 = "Hello";
String str3 = new String("Hello");
In the above example, str1 and str2 are string literals, and they are pointing to the same memory location, so str1 == str2 returns true. However, str3 is created using the new keyword, and it is pointing to a different memory location, so str1 == str3 returns false.
On the other hand, the equals() method is used to compare the contents of two objects to see if they are equal or not. It checks if two objects are equivalent or not. When two objects are compared using equals(), it returns true if the contents of both objects are the same, and false otherwise.
For example:
String str1 = "Hello";
String str2 = "Hello";
String str3 = new String("Hello");
In summary, the == operator compares the object references of two objects to see if they are pointing to the same memory location, while the equals() method compares the contents of two objects to see if they are equal or not.
Комментарии