Telegram Web Link
Code snippet:
Code snippet:
Code snippet:
Code snippet:
​​#interview_questions

What is the difference between equals() and == in Java?

Both equals() method and the == operator are used to compare two objects in Java. == is an operator and equals() is method. But == operator compares reference or memory location of objects in a heap, whether they point to the same location or not.
Whenever we create an object using the operator new, it will create a new memory location for that object. So we use the == operator to check memory location or address of two objects are the same or not.

In general, both equals() and “==” operators in Java are used to compare objects to check equality, but here are some of the differences between the two:

The main difference between the .equals() method and == operator is that one is a method, and the other is the operator. We can use == operators for reference comparison (address comparison) and .equals() method for content comparison. In simple words, == checks if both objects point to the same memory location whereas .equals() evaluates to the comparison of values in the objects. If a class does not override the equals method, then by default, it uses the equals(Object o) method of the closest parent class that has overridden this method.
Code snippet:
Assuming weather is a well-formed nonempty array, which code snippet, when inserted independently into the blank in the following code, prints all of the elements of weather? (Choose all that apply.)
Anonymous Poll
16%
int i = weather.length; i > 0; i--
55%
int i = 0; i <= weather.length - 1; ++i
18%
var w : weather
38%
int i = weather.length - 1; i >= 0; i--
19%
int i = 0, int j = 3; i < weather.length; ++i
9%
int i = 0; ++i < 10 && i < weather.length;
10%
None of the above
Code snippet:
2024/11/16 08:21:27
Back to Top
HTML Embed Code: