Java Core
Question 1 of 15Easy
What is the difference between == and equals() in Java ?
Answer
== compares references for objects (the memory address) and values for primitive types. equals() is a method that compares the logical content of two objects, and its default behavior in Object falls back to ==, unless the class overrides it (like String or the wrapper classes). To compare the content of two business objects, you always need to override equals() in the class itself.
Common trap
The classic trap is comparing two Strings with ==: it sometimes appears to work by accident thanks to the string pool, but it breaks as soon as one of the strings comes from new String(...) or dynamic concatenation.