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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:25:32+00:00 2026-06-05T06:25:32+00:00

Consider two references of type Integer that call the static factory method valueOf as

  • 0

Consider two references of type Integer that call the static factory method valueOf as shown below:-

    Integer a = Integer.valueOf("10"); 
    Integer b = Integer.valueOf("10"); 

Considering that Integer is immutable, is it ok to compare a and b using == instead of using equals method. I am guessing that the valueOf method makes sure that only one instance of Integer with the value 10 is created and a reference to this instance is returned for every Integer created with a value 10.

In general, is it ok to compare two references of an immutable class that are created using a call to the same static factory method by using == instead of equals?

Edit:
The Integer class was used just as an example. I am aware thar Intgers upto 127 will return true if they are compared using ==. What i need to know is that when l create my own immutable class, say MyImmutable with a method create() that will ensure that no duplicate MyImmutable objects are created, will it be ok if I compare 2 MyImmutable references created using the create method by using == instead of equals.

  • 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-06-05T06:25:33+00:00Added an answer on June 5, 2026 at 6:25 am

    No, that’s not safe in general. The == operator compares the references, not the values.

    Using == happens to work for integers between -128 and 127, but not for other integers. The following code demonstrates that == won’t always work:

    Integer a = Integer.valueOf(10); 
    Integer b = Integer.valueOf(10); 
    System.out.println(a == b);
    
    true
    
    Integer c = Integer.valueOf(1000); 
    Integer d = Integer.valueOf(1000); 
    System.out.println(c == d);
    
    false
    

    See it working online: ideone

    The explanation for this behaviour lies in the implementation of Integer.valueOf:

    public static Integer valueOf(int i) {
         final int offset = 128;
         if (i >= -128 && i <= 127) { // must cache
             return IntegerCache.cache[i + offset];
         }
         return new Integer(i);
     }
    

    source

    Not also that the standard requires that boxing integers for small inputs (-128 to 127) gives objects with equal references.

    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.

    However the standard makes no such guarantees for integers outside this range.


    In general, is it ok to compare two references of an immutable class that are created using a call to the same static factory method by using == instead of equals?

    As shown above, it won’t work in general. But if you ensure that two immutable objects with the same value always have the same reference, then yes, it could work. However there are some rules you must follow carefully:

    • The constructor must not be public.
    • Every object you create via the static method must be cached.
    • Every time you are asked to create an object you must first check the cache to see if you have already created an object with the same value.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: No type inference with generic extension method Consider two methods: public static
I ran across a compilation issue today that baffled me. Consider these two container
Consider the following two Java files that contain a simplified version of my issue
Consider the following two toolbars that are in the same project of mine: Notice
Consider the two following regular expression snippets and dummy HTML that it should be
D has two types of constness: immutable variables are ones that were declared immutable,
Consider two tables as mentioned below - Table1 (id_col, name_col) Table2 (uid_col, code_col_1, code_col_2)
Consider we have two tables ProductType and ProductSizeGroup as below ProductType Id Name MaleSizeGroupId
Consider two tables: Transactions , with amounts in a foreign currency: Date Amount =========
Consider two methods on the controller CustomerController.cs : //URL to be http://mysite/Customer/ public ActionResult

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.