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 4084300
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:25:20+00:00 2026-05-20T18:25:20+00:00

Hey I am trying to understand the following code snippet. public static void main(String[]

  • 0

Hey I am trying to understand the following code snippet.

public static void main(String[] args) {
    Integer i1 = 1000;
    Integer i2 = 1000;
    if(i1 != i2) System.out.println("different objects");
    if(i1.equals(i2)) System.out.println("meaningfully equal");
    Integer i3 = 10;
    Integer i4 = 10;
    if(i3 == i4) System.out.println("same object");
    if(i3.equals(i4)) System.out.println("meaningfully equal");
}

This method runs all of the println instructions. That is i1 != i2 is true, but i3 == i4. At first glance this strikes me as strange, they should be all different as references. I can figure out that if I pass the same byte value (-128 to 127) to i3 and i4 they will be always equal as references, but any other value will yield them as different.

I can’t explain this, can you point me to some documentation or give some helpful insights?

Thank you

  • 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-20T18:25:21+00:00Added an answer on May 20, 2026 at 6:25 pm

    Autoboxing int values to Integer objects will use a cache for common values (as you’ve identified them). This is specified in the JLS at §5.1.7 Boxing Conversion:

    If the value p being boxed is true, false, a byte, a char in the range \u0000 to \u007f, or an int or short number between -128 and 127, then let r1 and r2 be the results of any two boxing conversions of p. It is always the case that r1 == r2.

    Note that this will only be applied when the language auto-boxes a value for you or when you use Integer.valueOf(). Using new Integer(int) will always produce a new Integer object.

    Minor hint: a JVM implementation is free to cache values outside of those ranges as well, because the opposite is not specified. I’ve not yet seen such an implementation, however.

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

Sidebar

Related Questions

hey I have been trying for hours and I don't understand why the following
Hey guys i am trying to automate my system at work for sending artwork
Hey i'm just trying to write some code in DrScheme: ((function (x) (* x
Hey all i am trying to figiure out why my code is not triggering
Hey everyone, I am trying to run the following program, but am getting a
Hey guys, i'm really trying to understand regular expressions while scraping a site, i've
Hey guys I am having a lot of trouble trying to understand this and
Hey guys, I've been trying to understand the documentation and find an example, but
Hey so i'm trying to build the following member Functors of class ConcavePolygon ,
Hey Folks i am trying to compile this C++ program: #include <stdio.h> #include <string.h>

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.