Java Equals Operator
1. The result of Equality Operator is boolean either true or false.These are Equality operators in java.
= =, !=
2.We can apply Equality Operators for primitive type and boolean type.
example:-
System.out.println(10==20) ; output is false
System.out.println(10!=20) ; output is true
System.out.println('a'=='b') ; output is false
System.out.println('a'==97) ; output is true
System.out.println(false==false) ; output is true
3. We can apply equality operator for objects.
If both references are pointing to the same object then equality operator returns true.
If both references are not pointing to the same object then equality operator returns false.
example:-
Test t1=new Test( );
Test t2=new Test( );
Test t3=t1;
System.out.println(t1==t2) ; output is false
System.out.println(t1==t3) ; output is true
Difference between Equality Operator and .equals( ) method
In java Equality operators( == , != )are meant for reference comparison(address comparison).
.equals( ) method is meant for content comparison .
example:-
String s1= new String("javabychetan");
String s2 =new String ("javabychetan");
System.out.println(s1==s2) ; output is false
System.out.println(s1.equals(s2)) ; output is true
No comments:
Post a Comment
Be the first to comment!
Don't just read and walk away, Your Feedback Is Always Appreciated. I will try to reply to your queries as soon as time allows.
Note:
1. If your question is unrelated to this article, please use our Facebook Page.
2. Please always make use of your name in the comment box instead of anonymous so that i can respond to you through your name and don't make use of Names such as "Admin" or "ADMIN" if you want your Comment to be published.
Regards,
JavaByChetan
Back To Home