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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T06:52:58+00:00 2026-06-16T06:52:58+00:00

Today I was reviewing some code and I have been totally fooled. I saw

  • 0

Today I was reviewing some code and I have been totally fooled. I saw something like this in the code

...
NSNumber *myNumber = [NSNumber numberWithInteger:4];
...
if (myNumber == [NSNumber numberWithInteger:4)
{
...
}
...

When I read that if condition, my first thought was: waw man! how can you do that? You are obviously comparing pointers here, so of course the code in this if will never be executed as you are generating an instance in the right place, so the objects will never be the same!

My (big) surprise was that actually the if was being executed! So I generated some tests to deepen in what what was going on, and I got this:

NSInteger intValue = 5;
NSNumber *anIntNumber = [NSNumber numberWithInteger:intValue];
NSNumber *anotherIntNumber = [NSNumber numberWithInteger:intValue];
NSLog(@"%p vs %p -> %@", anIntNumber, anotherIntNumber, ((anIntNumber == anotherIntNumber) ? @"YES" : @"NO"));

This is the result:

0x7462560 vs 0x7462560 -> YES

So I then remembered that I read somewhere here in stack overflow that constant NSString were stored somewhere in the memory stack when a program begins. So then I run this code to check if the same thing was happening with NSString instances:

NSString *aString = @"FOO";
NSString *anotherString = @"FOO";
NSLog(@"%p vs %p -> %@", aString, anotherString, ((aString == anotherString) ? @"YES" : @"NO"));

This is the result:

0x35cc vs 0x35cc -> YES

So yes, the instance is the same although it was not intentional.
What I can imagine with these results is that the system allocates all the NSNumber objects instantiated with constant integers somewhere in the memory stack too.

But my question now is: why do they do that? Isn’t the waste of memory more important than the time to generate the instances?

Then I thought: what about the floats? Floating point numbers can not really be compared with the equal operator because of the binary representation and all that stuff. And here is the last test:

float floatValue = 5.41553f;
NSNumber *aFloatNumber = [NSNumber numberWithFloat:floatValue];
NSNumber *anotherFloatNumber = [NSNumber numberWithFloat:floatValue];
NSLog(@"%p vs %p -> %@", aFloatNumber, anotherFloatNumber, ((aFloatNumber == anotherFloatNumber) ? @"YES" : @"NO"));

And here is the only expected result!

0x712f180 vs 0x74299a0 -> NO

What can you say about this? Do you really think this is the best behavior (and the more logic)?
Thanks for sharing your knowledge!

  • 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-16T06:52:59+00:00Added an answer on June 16, 2026 at 6:52 am

    The use of the == operator with NSNumber only works in some cases because the NSNumber implementation internally uses a few global instances for the integers 0 through X (12 or 15 or something).

    The memory to hold a dozen NSNumber objects in memory is trivial.

    Regardless, never use the == operator to check equally. Always use isEqual: (or its variants). Never rely on implementation details or compiler optimizations like this.

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

Sidebar

Related Questions

Today in my code im downloading the images from a website like this: using
I was reviewing some code today and came across some code (accurately portrayed by
Okay this is a revised question from earlier today, I have included code to
Today morning I have noticed that some Javascript has been added to my hosted
Today i have a little problem containing a File Upload. First some Infos: Rubyversion:
Today I noticed a snippet of code that looks like the one below: public
Today I was reading code from some very popular numerical libraries written in FORTRAN
Today I was writing some C code to sort an array of structs using
Today, I saw this article on Twitter about getting Webm data from a canvas:
I was reviewing some today, when I encountered the following convention : TestParam(1); function

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.