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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:28:30+00:00 2026-05-14T21:28:30+00:00

Consider the following snippet: int i = 99999999; byte b = 99; short s

  • 0

Consider the following snippet:

    int i = 99999999;
    byte b = 99;
    short s = 9999;
    Integer ii = Integer.valueOf(9); // should be within cache

    System.out.println(new Integer(i) == i); // "true"
    System.out.println(new Integer(b) == b); // "true"
    System.out.println(new Integer(s) == s); // "true"
    System.out.println(new Integer(ii) == ii); // "false"

It’s obvious why the last line will ALWAYS prints "false": we’re using == reference identity comparison, and a new object will NEVER be == to an already existing object.

The question is about the first 3 lines: are those comparisons guaranteed to be on the primitive int, with the Integer auto-unboxed? Are there cases where the primitive would be auto-boxed instead, and reference identity comparisons are performed? (which would all then be false!)

  • 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-14T21:28:31+00:00Added an answer on May 14, 2026 at 9:28 pm

    Yes. JLS §5.6.2 specifies the rules for binary numeric promotion. In part:

    When an operator applies binary
    numeric promotion to a pair of
    operands, each of which must denote a
    value that is convertible to a numeric
    type, the following rules apply, in
    order, using widening conversion
    (§5.1.2) to convert operands as
    necessary:

    If any of the operands is of a
    reference type, unboxing conversion
    (§5.1.8) is performed.

    Binary numeric promotion applies for several numeric operators, including "the numerical equality operators == and !=."

    JLS §15.21.1 (Numerical Equality Operators == and !=) specifies:

    If the operands of an equality
    operator are both of numeric type, or
    one is of numeric type and the other
    is convertible (§5.1.8) to numeric
    type, binary numeric promotion is
    performed on the operands (§5.6.2).

    In contrast, JLS §15.21.3 (Reference Equality Operators == and !=) provides:

    If the operands of an equality
    operator are both of either reference
    type or the null type, then the
    operation is object equality

    This fits the common understanding of boxing and unboxing, that’s it only done when there’s a mismatch.

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

Sidebar

Related Questions

Consider the following snippet: $(document).bind('mousemove', function(e) { $('#someDiv').css({left: e.pageX+'px', top: e.pageY+'px'}); }); This should
Consider the following snippet: #include <map> class A { static std::map<int,int> theMap; #pragma omp
Consider the following code snippet: class Test { public int Length{ get; set; }
Consider the following code snippet: std::vector<int> v; v.reserve(100); v.insert(v.end(), 100, 5); v.erase(v.begin(), v.end()); std::cout
Consider the following code snippet namespace ConsoleApplication1 { public delegate TResult Function<in T, out
Consider the following code-snippet typedef int type; int main() { type *type; // why
Consider the following snippet: class Foo { public: Foo( int Value ); // other
Consider the following snippet: using System; using System.Collections.Generic; using System.Linq; using System.Net; namespace ForumLogins
Consider the following code snippet: int index = FindClosestIndex(frame); if (_data[index].Frame == frame) return
Consider the following snippet: Calendar futureDate = Calendar.getInstance(); int year = 2011; int month

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.