aspose file tools*
The moose likes Beginning Java and the fly likes operator and function( ) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "operator and function( )" Watch "operator and function( )" New topic
Author

operator and function( )

saransh panda
Greenhorn

Joined: May 02, 2008
Posts: 2
for comparison between two objects we can use
[1] operator 'obj1==obj2'
[2] function 'obj1.equals(obj2)'
but what is the difference between them? And if they represent the same funcationality what is the significance of equals() function?

thankyou!
Bryan Snipes
Greenhorn

Joined: May 09, 2008
Posts: 4
I'm just a newbie myself, but I'll try to answer (also for my own learning).
The "==" operator is for comparing two primitive variables.
The ".equals" is used for comparing the value of two objects.

I'm sure the gurus around here can add more explanation.
Balasubramanian Chandrasekaran
Ranch Hand

Joined: Nov 28, 2007
Posts: 215

Originally posted by saransh panda:
for comparison between two objects we can use
[1] operator 'obj1==obj2'
[2] function 'obj1.equals(obj2)'
but what is the difference between them? And if they represent the same funcationality what is the significance of equals() function?

thankyou!


the == operator actually tests the equality of the reference variables, that is, if the two variables are referring to the same object. To test the logical equality, that is, the equality of the object's state, we normally use the equals() method.

And welcome to the ranch
[ May 12, 2008: Message edited by: Balasubramanian Chandrasekaran ]
saransh panda
Greenhorn

Joined: May 02, 2008
Posts: 2
thanks for the reply bryan
but I think '==' operator can be used for comparing objects as well as primitives. In fact I have tried it and it works on objects as well.
Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
Here is the copy and paste from my old post.

Here is the quick difference between == and equals():

== checks wheather two object refernces points to same instance or not.
Whereas equals() compare the actual contents of two string objects.


Example:

Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3548
    
  15
Originally posted by Vishal Pandya:
== checks wheather two object refernces points to same instance or not.
Whereas equals() compare the actual contents of two string objects.


This is true for Strings but not for all objects. If a class does not override the equals method then it will inherit the equals method from the Object class. The equals method in the Object class compares the two objects using ==.


Joanne
 
 
subject: operator and function( )