Q. What is difference between == and equals() while comparing strings? Which one
Both equals() method and == operator is used to compare two objects in Java. == is an operator and equals() is method. But == operator compare reference or memory location of objects in the heap, whether they point to the same location or not .
Equal is more reliable to the strings.It is also compare the value of variables
== operater is used for referenced variable wise comparison if both are equal send created is duplication. If is false search datawisecomparision using equals().
How to post free Internship ads?
1.== is a operator.equals() is a function .
2.== only checks the primitive value but equals() checks the primitive value as well as the memory location for the variable (String).
3.== is not suitable for string comparison but we can do the same work by using equals().
4. == is used for comparing the reference object and equals() used for comparing the heap object.
i think equals() operator is more reliable as == operator compares the memory address of the variables whereas equals compares the value of the variables.
== it is reliable because it compares the two strings
Both == operator and equals() for comparing objects in java.
But == operator we are using for comaring object reference in the heap.
But equals() is to compare the contain of the object in heap.
ex- String s1=new String("grv");
String s1=new String("grv");
Main difference between == and equals in Java is that "==" is used to compare primitives while equals() method is recommended to check equality of objects. equals() is more reliable to compare the strings .
== is a relational operator.It is used for compare two values. While,equals() is used to check the two strings ,if its equal it return true ,but if it is unequal it return false . In other words it check the equality of two objects.