• Post Reply Bookmark Topic Watch Topic
  • New Topic

.equals is not working as expected.  RSS feed

 
Shelby Simpson
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To my understanding == compares that they are the same object while .equals() compares that the content is the same. So I created the code below to make sure I understand the difference between == and .equals() To my understanding in this code only the if statement with .equals should print out but they both print. Not sure what is going on here any help would be appreciated.

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try it with a larger number, like 1234.

Java has constant pools that cache certain objects. For instance, String literals go into the String pool, so if you have "abc" twice in your code, there's only one object, and == will return true.

There are also cached instances for small values of the integral type wrappers. I think the default is -128..127, but it's configurable by a JVM startup option. Any time you use autoboxing, or call the appropriate valueOf() method (which is what autoboxing does), it will retrieve the cached instance if it's present, else create a new one.

So when you do Integer one = 1; it just gets a reference to the cached object, rather than creating a new Integer object. So every case of Integer something = 1; will be pointing to the same object.
 
Mansukhdeep Thind
Ranch Hand
Posts: 1163
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:Try it with a larger number, like 1234.
.... I think the default is -128..127, but it's configurable by a JVM startup option.


Yes, that is the default range Jeff. How do I configure this on JVM start-up? What is JVM start up first of all? When do I say that my "JVM has started"?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mansukhdeep Thind wrote:How do I configure this on JVM start-up?


java -Xsome_arg_for_wrapper_caching_range_you_have_to_look_it_up_for_your_jvm MyClass

What is JVM start up first of all?


It's when you execute java on the command line.

When do I say that my "JVM has started"?


From outside: When you have executed Java on the command line.

From inside: When your main() method starts executing.
 
Mansukhdeep Thind
Ranch Hand
Posts: 1163
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks chief.
 
Campbell Ritchie
Marshal
Posts: 56530
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It all goes to show that you should avoid ==. You will find more about that particular pitfall in the Integer#valueOf(int) method and in the Java Language Specification. It isn’t easy to read, I am afraid.
 
Mansukhdeep Thind
Ranch Hand
Posts: 1163
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:It all goes to show that you should avoid ==. You will find more about that particular pitfall in the Integer#valueOf(int) method and in the Java Language Specification. It isn’t easy to read, I am afraid.


Darn it! I just saw the JLS link. I am afraid too. So many use cases.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shelby Simpson wrote:To my understanding in this code only the if statement with .equals should print out but they both print. Not sure what is going on here any help would be appreciated.

You may find the page about CachedObjects useful; but basically, Campbell is right: AvoidTheEqualityOperator (←click).

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!