• Post Reply Bookmark Topic Watch Topic
  • New Topic

int cannot be dereferenced  RSS feed

 
Vishnu Sharma
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried using equals() method for comparing two integer values & i am getting this compile time error:
Equaltest.java:9: int cannot be dereferenced
Is that mean, we can not use equals() with integer values??
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. equals() is a method of Object. As ints are primitives, they are not objects and do not inherit from Object.
 
Dhan Kumar
Greenhorn
Posts: 29
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
adding to that..
you can use equal method on Integer(which is object) and not on int(which is primitive).
 
Jonas Isberg
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you really like equals instead of ==, you better go and create some objects.
 
Rob Spoor
Sheriff
Posts: 21131
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jonas Isberg wrote:

Surely you mean this:
This is slightly better for performance since it will return the very same Integer objects for any value between -128 and 127 (inclusive). Using "new Integer", "new Boolean", "new Long", "new Double" etc is discouraged since Java 5.0 and auto-boxing. Not that it's wrong, it's just that there is a better alternative these days.
 
Jonas Isberg
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:Surely you mean this:

After reading your post I surely do. A static constructor (is that the correct name?)
surely can more trix than me. :-)

The only reasons for not keeping the primitiv ints and use == would be polymorfic
ones, right?
 
Rob Spoor
Sheriff
Posts: 21131
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jonas Isberg wrote:
Rob Prime wrote:Surely you mean this:

After reading your post I surely do. A static constructor (is that the correct name?)
surely can more trix than me. :-)

I believe that factory method is the correct name.

The only reasons for not keeping the primitiv ints and use == would be polymorfic ones, right?

And using them anywhere that only objects are allowed, like in collections or reflection.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!