• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

== and equals Problem

 
Ranch Hand
Posts: 634
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 276
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 247
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
reply
    Bookmark Topic Watch Topic
  • New Topic