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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T15:53:57+00:00 2026-05-10T15:53:57+00:00

As a Java developer who is reading Apple’s Objective-C 2.0 documentation: I wonder what

  • 0

As a Java developer who is reading Apple’s Objective-C 2.0 documentation: I wonder what ‘sending a message to nil‘ means – let alone how it is actually useful. Taking an excerpt from the documentation:

There are several patterns in Cocoa that take advantage of this fact. The value returned from a message to nil may also be valid:

  • If the method returns an object, any pointer type, any integer scalar of size less than or equal to sizeof(void*), a float, a double, a long double, or a long long, then a message sent to nil returns 0.
  • If the method returns a struct, as defined by the Mac OS X ABI Function Call Guide to be returned in registers, then a message sent to nil returns 0.0 for every field in the data structure. Other struct data types will not be filled with zeros.
  • If the method returns anything other than the aforementioned value types the return value of a message sent to nil is undefined.

Has Java rendered my brain incapable of grokking the explanation above? Or is there something that I am missing that would make this as clear as glass?

I do get the idea of messages/receivers in Objective-C, I am simply confused about a receiver that happens to be nil.

  • 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. 2026-05-10T15:53:57+00:00Added an answer on May 10, 2026 at 3:53 pm

    Well, I think it can be described using a very contrived example. Let’s say you have a method in Java which prints out all of the elements in an ArrayList:

    void foo(ArrayList list) {     for(int i = 0; i < list.size(); ++i){         System.out.println(list.get(i).toString());     } } 

    Now, if you call that method like so: someObject.foo(NULL); you’re going to probably get a NullPointerException when it tries to access list, in this case in the call to list.size(); Now, you’d probably never call someObject.foo(NULL) with the NULL value like that. However, you may have gotten your ArrayList from a method which returns NULL if it runs into some error generating the ArrayList like someObject.foo(otherObject.getArrayList());

    Of course, you’ll also have problems if you do something like this:

    ArrayList list = NULL; list.size(); 

    Now, in Objective-C, we have the equivalent method:

    - (void)foo:(NSArray*)anArray {     int i;     for(i = 0; i < [anArray count]; ++i){         NSLog(@'%@', [[anArray objectAtIndex:i] stringValue];     } } 

    Now, if we have the following code:

    [someObject foo:nil]; 

    we have the same situation in which Java will produce a NullPointerException. The nil object will be accessed first at [anArray count] However, instead of throwing a NullPointerException, Objective-C will simply return 0 in accordance with the rules above, so the loop will not run. However, if we set the loop to run a set number of times, then we’re first sending a message to anArray at [anArray objectAtIndex:i]; This will also return 0, but since objectAtIndex: returns a pointer, and a pointer to 0 is nil/NULL, NSLog will be passed nil each time through the loop. (Although NSLog is a function and not a method, it prints out (null) if passed a nil NSString.

    In some cases it’s nicer to have a NullPointerException, since you can tell right away that something is wrong with the program, but unless you catch the exception, the program will crash. (In C, trying to dereference NULL in this way causes the program to crash.) In Objective-C, it instead just causes possibly incorrect run-time behavior. However, if you have a method that doesn’t break if it returns 0/nil/NULL/a zeroed struct, then this saves you from having to check to make sure the object or parameters are nil.

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

Sidebar

Ask A Question

Stats

  • Questions 60k
  • Answers 61k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer My personal belief is that you should design the database… May 11, 2026 at 9:35 am
  • added an answer This recommendation comes from the best practice of 'separation of… May 11, 2026 at 9:35 am
  • added an answer Have a look at that list of Code Coverage Tools… May 11, 2026 at 9:35 am

Related Questions

As a Java developer who is reading Apple's Objective-C 2.0 documentation: I wonder what
My background is primarily as a Java Developer, but lately I have been doing
As it stands now, I'm a Java and C# developer. The more and more
I have a Java application set up as a service to do data-mining against
I have a paint application that runs as a Java applet. I need to
I've just inherited a java application that needs to be installed as a service
What's the easiest way to centre a java.awt.Window , such as a JFrame or
I am looking for the best method to run a Java Application as a
The log4j network adapter sends events as a serialised java object. I would like
A related post here pretty much established reflection in Java as a performance hog.

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.