• 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

Does == on two Integers depend on the value stored?

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Java ranchers,

I was doing a mock exam and came across this


public class Troll
{
Integer gruesomeness;
String flatness;
public static void main(String[] args)
{
Troll troll = new Troll();
Troll troll2 = new Troll();
troll.gruesomeness = 98762;
troll2.gruesomeness = 98762;
troll.flatness="flat";
troll2.flatness="flat";
System.out.println(troll.equals(troll2)); //#1
}

public boolean equals(Object arg0)
{
if (arg0 instanceof Troll)
{
Troll new_name = (Troll) arg0;
return gruesomeness==new_name.gruesomeness && new_name.flatness.equals(flatness);
}
return false;
}

public int hashCode()
{
return 0;
}
}


This prints out false at //#1 for all the values > 127. Why is that?

 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Comparing Integers with == is not recommended. Use a primitive or equals.

The integer class holds a buffer for integer between the range -128 and 127 by default or the value set with the java.lang.Integer.IntegerCache.high property with a minimum of 127.

Because of the cache Integers created by autoboxing within the range are the some objects thus returning true for the == comparison. Integer obtained otherwise or not in the range, are not cached thus a new object will be created.

// And welcome to the JavaRanch. Please read our naming policy.
 
L K Kakani
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it the same with other Wrapper classes too?? I understand that its not suggested to use == for Wrappers, but that is inconsistent behavior.
Are we supposed to look out for such questions ?? Guess its tough to take on such ones , hopefully we don't come across these kind in the SCJP exam. Thanks for your reply..
 
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
• Two instances of the wrapper objects Boolean, Byte, Character from \u0000 to \u007f and short and integer from -128 to 127 will always be == when their primitive values are same
 
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The == operator should not be used to test for "meaningful equivalence"
of objects. That a small value Integer object is cached by the JVM as a
performance optimization should not be exploited in this way. Rather,
ob1.equals(ob2) should always be used to compare objects.

Jim ... ...
 
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"java time"

Please check your private messages regarding an important administrative matter.

-Andrew
 
Ranch Hand
Posts: 623
1
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@K Kakani

You could also try Integer.valueOf(...) instead of autoboxing and check the results of:
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Autoboxing uses the valueOf method to obtain the Wrapper object.
 
L K Kakani
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all for taking time to post replies. It was very helpful.
 
Honk if you love justice! And honk twice for tiny ads!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic