This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes == operator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "== operator" Watch "== operator" New topic

== operator

Gaurav Pavan Kumar Jain
Ranch Hand

Joined: Mar 19, 2007
Posts: 168
hi all

please let me know the functioning of == operator in refrence of object variable. Please specify example also
Qunfeng Wang
Ranch Hand

Joined: Jan 28, 2005
Posts: 434
The "==" operator is used to determine if two references refer to the same object when object variables concerned.

To be or not to be. It's a question.
Saket Barve
Ranch Hand

Joined: Dec 19, 2002
Posts: 229
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

Joined: Mar 19, 2007
Posts: 168
Thank you for your suggestion
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1090
By default equals() would also check for reference equality unless it is overridden.
sadasiva kumar
Ranch Hand

Joined: Oct 30, 2003
Posts: 91
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 }


A bend in the road is not the end of the road ... unless you fail to make the turn.
I agree. Here's the link:
subject: == operator
jQuery in Action, 3rd edition