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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:10:18+00:00 2026-06-01T02:10:18+00:00

Is there any different between NSString * str = @123; and NSString * str

  • 0

Is there any different between

NSString * str = @"123";

and

NSString * str = [[NSString alloc] initWithString:@"123"];

from compiler’s aspect?

  • 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-01T02:10:20+00:00Added an answer on June 1, 2026 at 2:10 am

    Theoretically yes; in implementation detail, probably not.

    In the first case, the compiler creates a constant string and assigns a pointer to it to the variable str. You do not own the string.

    In the second case, the compiler creates a constant string (as before) but this time it is used by the run time as a parameter in initialising another string that you do own (because the second string was created using alloc).

    That’s the end of the stuff you need to know.

    However, there is a lot of optimisation that goes on. Because NSStrings are immutable, you’ll find that initWithString: actually just returns the parameter. Normally, it would retain the parameter before returning it to you (because you are expecting to own it) but literal strings have a special retainCount (INT_MAX I think) to stop the run time from ever trying to deallocate them. So in practice, your second line of code produces identical results to the first.

    This incidentally, is why it is incorrect top say the string is autoreleased in the first case, because it isn’t. It’s just a constant string with a special retain count.

    But you can and should safely ignore the implementation detail and just remember, you don’t own the string in the first case, but you do own it in the second case.

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

Sidebar

Related Questions

In javascript, is there any different between these two: // call MyFunction normal way
Is there any performance different between hosting your asp.net in mono on linux and
is there any different in Connection string between SQL server Authentication and Windows Authentication
Is there any c# library that handles lossless conversion between different image formats?
I was just wondering if there is any difference between the two different new
I was just curious, is there any difference between the following two codes? NSString
Is there any different between declaring event Action<> and event EventHandler<> . Assuming it
Is there any different between for(;;) and for(:) in terms of performance in Objective-C?
Is there any difference between int on_exit(void (*function)(int , void *), void *arg); and
Is there any difference between: if foo is None: pass and if foo ==

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.