• 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

.equals is not working as expected.

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks chief.
 
Marshal
Posts: 79039
375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic