Computer Science, asked by Navena2310, 1 year ago

difference between = and == operators in java

Answers

Answered by Anonymous
79
Difference between == and equals in Java

Main difference between == and equals in Java is that "==" is used to compare primitives while equals() method is recommended to check equality of objects. Another difference between them is that, If both "==" and equals() is used to compare objects than ==returns true only if both references points to same object while equals() can return true or false based on its overridden implementation.One of the popular cases is comparing two String in Java in which case == and equals() method return different results.

Answered by ericksonrjoseppcsukt
154

single equals "=" and double equals "==" are different in multiple languages including C,C++,Java, Javascript, etc.

The difference is that = sets a variable to the given value, and == compares the value stored in a variable with the given value. For example:

x = 2 (now the value 2 is set to x)

x == 3 (checks if the value in x is 3; if so, returns true, else returns false) will return false, as the value of x is 2.

Similar questions