my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes == and equals Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "== and equals Problem" Watch "== and equals Problem" New topic
Author

== and equals Problem

Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634



error:

long cannot be dereferenced
if(x.equals(w))
^

can anyone explain why am i getting this error ?

i am confused in == and equals method.
please give a link where can i study these topics.


OCPJP 6.0 93%
OCPJWCD 5.0 98%
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61229
    
  66

.equals() can never be applied to primitives -- they don't possess methods at all. Use == to compare primitives.

Object references, on the other hand, use == to test for identity, and .equals() to test for equality.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

long is a primitive and not an Object. equals() is a method and it can be called only on a method.
For comparing the equality of primitives, we always use '=='.
If you want, you can try equals() method after wrapping it with java.lang.Long wrapper class.


OCPJP 6
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61229
    
  66

Vinoth Kumar Kannan wrote:equals() is a method and it can be called only on a method.

Huh? Methods do not have methods (in Java). Did you mean something else?
Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

please give me a useful link where can i study difference between
== and equals

and know about them in detail.
Ram Narayan.M
Ranch Hand

Joined: Jul 11, 2010
Posts: 247

Seconding Bear,

methods cannot be called on primitives... To treat primitive values as Objects, here comes Wrapper classes... It can be called on Wrapper class instance.


Complementing Vinod's comments,

== is mainly used for just checking the primitive variable contents equality...

But in case of reference variable to objects is the different case

With == , it can be checked as the reference variables referring to same object or not.

For reference variables to objects , equals() method can be used to check if objects are equal meaningfully.

SCJP 6 [SCJP - Old is Gold]
Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

Bear Bibeault wrote:
Vinoth Kumar Kannan wrote:equals() is a method and it can be called only on a method.

Huh? Methods do not have methods (in Java). Did you mean something else?

Oops a typing err...equals() is a method and it can be called only on a Object! Sorry about that! I had something in mind, and typed something else! I'm right now laughing at myself for doing that - calling a method on a method! lol
Gotta get some real sleep, I guess!
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

mohitkumar gupta wrote:please give me a useful link where can i study difference between
== and equals

and know about them in detail.


Simply, where are the methods in java defined? In classes, interfaces. Do primitives have classes? Then how can you invoke methods on them? And have a Google search, you can find a lot.


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

You may also find a question in the exam that try to use equals() method to compare or compareTo() method to sort applied on primitives.
Be sneaky in the code and run towards the option thats says compilation failure


SCJP6.0,My blog Ranchers from Delhi
 
wood burning stoves
 
subject: == and equals Problem