• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

== operator

 
Gaurav Pavan Kumar Jain
Ranch Hand
Posts: 168
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all

please let me know the functioning of == operator in refrence of object variable. Please specify example also
 
Qunfeng Wang
Ranch Hand
Posts: 434
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The "==" operator is used to determine if two references refer to the same object when object variables concerned.
 
Saket Barve
Ranch Hand
Posts: 229
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The == operator will check for reference equality whereas the .equals method will compare the actual values.

In the following code, "Different Reference" will be printed out.



Here's a good link.
 
Gaurav Pavan Kumar Jain
Ranch Hand
Posts: 168
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your suggestion
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By default equals() would also check for reference equality unless it is overridden.
 
sadasiva kumar
Ranch Hand
Posts: 91
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use == operator to compare two primitives,or to see if two references refer to the same object.
Remember ,the == operator cares only about the pattern of bits in the variable.The rules are the same whether the variable is a reference or primitive.

Example : (1) Comparing two primitives

int a = 9;
byte b = 9;
if( a == b) { // true }

(2) Comparing two references

Zoom a = new Zoom();
Zoom b = new Zoom();
Zoom c = a;
if(a == b) { // false }
if(a == c) { // true }
if(b == c) { // false }

regards
SADASIVAKUMAR UTTI
SCJP1.4
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic