difference between = and == in java
Answers
Thanks for asking the question!
Here is your answer : -
= is a assignment operator whereas == is a relational operator.
What are assignment operators?
- Assignment operators ( = ) is used to assign a value to a variable/constant . It assigns the value on it's right side to the variable written to the left of it . The result will be the newly assigned value to the variable on the left hand side of the expression.
Syntax : <variable>=<expression>
What are relational operators?
- Relational operators are used to show the relationship among the operands. These operators compare the values of variables and determine the result in the boolean expression, either true or false . Java provides six types of relational operators.
Syntax : < variable 1 > relational operator < variable 2 >
Hope it helps you ^_^
Hope u like it a lot dear...
= is for assignment but == checks equivalence and returns true or false. It’s also important, while on this topic, to point out the difference between .equals() and ==
For primitive data types, == checks equivalence as you would expect. However, for objects, == checks whether the references are the same (i.e. the two objects being compared are in fact the same object), not whether the contents are equivalent.
If you want to know that two distinct objects, strings, etc. have the same content when they are separate instances of the same type, you must use the .equals() method on the first object and pass the second object as the parameter, to determine whether their contents are the same. Furthermore, the class must have an overriding implementation of .equals that makes sense for that class, as without an explicit implementation of the method, all objects inherit the default implementation from Object, which is simply to compare the references just like ==.