Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

== and equals Problem

 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64188
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
.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.
 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Chrome Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64188
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 634
Chrome Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 247
Chrome Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Chrome Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2066
Clojure IntelliJ IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic