Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7527607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:08:52+00:00 2026-05-30T04:08:52+00:00

Possible Duplicate: Inconsistent behavior on java's == Integer wrapper objects share the same instances

  • 0

Possible Duplicate:
Inconsistent behavior on java's ==
Integer wrapper objects share the same instances only within the value 127?

I have found the following == behaviour for Integer objects and I fail to understand it. (I am well aware that one should use equals for such comparisons, but I am studying for OCPJP…)

On short, == works as expected for 1000, but not for 10.

The former fragment of code is:

Integer i1 = 1000;
Integer i2 = 1000;
if(i1 != i2) System.out.println("different objects");
if(i1.equals(i2)) System.out.println("meaningfully equal");

and it behaves as one would expect:

different objects
meaningfully equal

The latter though:

Integer i3 = 10;
Integer i4 = 10;
if(i3 == i4) System.out.println("same object");
if(i3.equals(i4)) System.out.println("meaningfully equal"); 

has the following output:

same object
meaningfully equal

Can someone please explain why this is happening?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T04:08:53+00:00Added an answer on May 30, 2026 at 4:08 am

    Since Java 5, wrapper class caching was introduced. The following is an examination of the cache created by an inner class, IntegerCache, located in the Integer cache. For example, the following code will create a cache:

    Integer myNumber = 10
    

    or

    Integer myNumber = Integer.valueOf(10);
    

    256 Integer objects are created in the range of -128 to 127 which are all stored in an Integer array. This caching functionality can be seen by looking at the inner class, IntegerCache, which is found in Integer:

    So when creating an object using Integer.valueOf or directly assigning a value to an Integer within the range of -128 to 127 the same object will be returned. Therefore, consider the following example:

    Integer i = 100;
    Integer p = 100;
    if (i == p)  
        System.out.println("i and p are the same.");
    if (i != p)
        System.out.println("i and p are different.");   
    if(i.equals(p))
        System.out.println("i and p contain the same value.");
    

    The output is:

    i and p are the same.
    i and p contain the same value.
    

    It is important to note that object i and p only equate to true because they are the same object, the comparison is not based on the value, it is based on object equality. If Integer i and p are outside the range of -128 or 127 the cache is not used, therefore new objects are created. When doing a comparison for value always use the “.equals” method. It is also important to note that instantiating an Integer does not create this caching.

    Remember that “==” is always used for object equality, it has not been overloaded for comparing unboxed values

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Integer wrapper objects share the same instances only within the value 127?
Possible Duplicate: Pre & post increment operator behavior in C, C++, Java, & C#
Possible Duplicate: Java - HashMap vs Map objects What's the difference between : Map
Possible Duplicate: How do you send email from a Java app using Gmail? How
Possible Duplicate: array_splice() for associative arrays How to add an array value to the
Possible Duplicate: PHP get all arguments as array? Within a javascript function arguments always
Possible Duplicate: In Javascript, why is the “this” operator inconsistent? I have the following
Possible Duplicate: How do I make a list with checkboxes in Java Swing? I
Possible Duplicate: How an uninitialised variable gets a garbage value? So when an undefined
Possible Duplicate: Find out the instance id from within an ec2 machine I am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.