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

  • Home
  • SEARCH
  • 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 869655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T10:20:39+00:00 2026-05-15T10:20:39+00:00

I just saw code similar to this: public class Scratch { public static void

  • 0

I just saw code similar to this:

public class Scratch
{
    public static void main(String[] args)
    {
        Integer a = 1000, b = 1000;
        System.out.println(a == b);

        Integer c = 100, d = 100;
        System.out.println(c == d);
    }
}

When ran, this block of code will print out:

false
true

I understand why the first is false: because the two objects are separate objects, so the == compares the references. But I can’t figure out, why is the second statement returning true? Is there some strange autoboxing rule that kicks in when an Integer’s value is in a certain range? What’s going on here?

  • 1 1 Answer
  • 1 View
  • 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-15T10:20:39+00:00Added an answer on May 15, 2026 at 10:20 am

    The true line is actually guaranteed by the language specification. From section 5.1.7:

    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.

    The discussion goes on, suggesting that although your second line of output is guaranteed, the first isn’t (see the last paragraph quoted below):

    Ideally, boxing a given primitive
    value p, would always yield an
    identical reference. In practice, this
    may not be feasible using existing
    implementation techniques. The rules
    above are a pragmatic compromise. The
    final clause above requires that
    certain common values always be boxed
    into indistinguishable objects. The
    implementation may cache these, lazily
    or eagerly.

    For other values, this formulation
    disallows any assumptions about the
    identity of the boxed values on the
    programmer’s part. This would allow
    (but not require) sharing of some or
    all of these references.

    This ensures that in most common
    cases, the behavior will be the
    desired one, without imposing an undue
    performance penalty, especially on
    small devices. Less memory-limited
    implementations might, for example,
    cache all characters and shorts, as
    well as integers and longs in the
    range of -32K – +32K.

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

Sidebar

Related Questions

I just saw this piece of javascript code at Facebook: javascript:eval(function(p,a,c,k,e,r){e=function(c){return c.toString(a)};if(!''.replace(/^/,String)){while(c--)r[e(c)]=k[c]||e(c);k=[function(e){return r[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return
I just saw this code while studying the wordpress source code (PHP), You can
I just saw someone post the following, in a rant on ugly code: public
I just saw this code: artist = (char *) malloc(0); ...and I was wondering
Just now,I saw some code like this: if(condition){ var xx='sss'; } //do something if(condition){
I just saw this block of code on the Wikipedia article on conditional operators:
I just saw a demo that had this jquery code to show and hide
I just saw this in a project I downloaded from Code Project: base.DialogResult =
I just saw this code if ((fsi.Attributes & FileAttributes.Directory) == FileAttributes.Directory) { //... }
I just saw this source code on a website, but I don't know what

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.