Difference between equals() and ==
Hi all , these days i\'m about to write a post on very fashionable interview question that is what\'s the difference between equals and == .It is asked in every object familiarised language interview whether or not its Java or ASP.NET For this you must apprehend the essential difference between each .
1. The very first difference in both is that equals () is a method == is an operator . this can be the terribly basic factor you must understand equals and ==
As in java there\'s no concept of operator overloading thus we cannot modify the behavior of == operator.
2. “= = “operator : “= =” is a binary operator used to compare both primitive sorts like int ,Boolean and objects .
While examination primitive sort its works properly and returns right output
For example :
int a=1;
int b=2;
if(a==b)
else
Output : Not Equal;
While examination 2 objects using “==” ,a problem arises is that “= =” checks reference of each objects not price and returns output correspondingly .
This can be illustrated with the assistance of associate example :
String variable1=new String(“First”);
String variable2=new String(“First);
If(variable1==variable2)
else
Output: Not Equal
In the example above as variable1 and variable2 ar referring to 2 totally different objects despite of same price .Accordingly “==” operator can come false compared each
String variable1=new String(“First”);
String variable2=variable1;
If(variable1==variable2)
else
Output: Equal
In the example above as variable1 and variable2 ar referring to 2 same objects .Accordingly “==” operator can come true compared each
3. equals () : equals() method is used to compares the worth of 2 objects . this method is defined in Object class and can even be overridden to change the working
String variable1=new String(“First”);
String variable2=variable1;
If(variable1.equals(variable2))
else
Output: Equal
In the example above as variable1 and variable2 are referring to 2 totally different objects with same price .Accordingly equals() will come true compared each.
you may like :
How objects are stored in heap ?
1. The very first difference in both is that equals () is a method == is an operator . this can be the terribly basic factor you must understand equals and ==
As in java there\'s no concept of operator overloading thus we cannot modify the behavior of == operator.
2. “= = “operator : “= =” is a binary operator used to compare both primitive sorts like int ,Boolean and objects .
While examination primitive sort its works properly and returns right output
For example :
int a=1;
int b=2;
if(a==b)
else
Output : Not Equal;
While examination 2 objects using “==” ,a problem arises is that “= =” checks reference of each objects not price and returns output correspondingly .
This can be illustrated with the assistance of associate example :
String variable1=new String(“First”);
String variable2=new String(“First);
If(variable1==variable2)
else
Output: Not Equal
In the example above as variable1 and variable2 ar referring to 2 totally different objects despite of same price .Accordingly “==” operator can come false compared each
String variable1=new String(“First”);
String variable2=variable1;
If(variable1==variable2)
else
Output: Equal
In the example above as variable1 and variable2 ar referring to 2 same objects .Accordingly “==” operator can come true compared each
3. equals () : equals() method is used to compares the worth of 2 objects . this method is defined in Object class and can even be overridden to change the working
String variable1=new String(“First”);
String variable2=variable1;
If(variable1.equals(variable2))
else
Output: Equal
In the example above as variable1 and variable2 are referring to 2 totally different objects with same price .Accordingly equals() will come true compared each.
you may like :
How objects are stored in heap ?
No comments:
Post a Comment