Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are NULL and nil equivalent?

Actually my question here is: are null and nil equivalent or not?

I have an example but I am confused when they are equal when they are not.

NSNull *nullValue = [NSNull null];
NSArray *arrayWithNull = [NSArray arrayWithObject:nullValue];
NSLog(@"arrayWithNull: %@", arrayWithNull);
id aValue = [arrayWithNull objectAtIndex:0];

if (aValue == nil) {
    NSLog(@"equals nil");
} else if (aValue == [NSNull null]) {
    NSLog(@"equals NSNull instance");
    if ([aValue isEqual:nil]) {
        NSLog(@"isEqual:nil");
    }
}

Here in the above case it shows that both null and nil are not equal and it displays "equals NSNull instance"

NSString *str=NULL;
id str1=nil;
if(str1 == str)
{
   printf("\n IS EQUAL........");

}
else
{
    printf("\n NOT EQUAL........");
}

And in the second case it shows both are equal and it displays "IS EQUAL".

Anyone's help will be much appreciated.

Thank you, Monish.

like image 583
monish Avatar asked Mar 17 '10 10:03

monish


People also ask

Is nil the same as NULL Ruby?

Let's start out with “Nil” since it's the most common and easy-to-understand way of representing nothingness in Ruby. In terms of what it means, Nil is exactly the same thing as null in other languages.

What is difference between NULL and nil in Swift?

The difference is that while NULL represents zero for any pointer, nil is specific to objects (e.g., id) and Nil is specific to class pointers.

Is nil same as?

Nil means the same as zero. It is usually used to say what the score is in sports such as rugby or football. They beat us one-nil in the final. If you say that something is nil, you mean that it does not exist at all.

Is same as nil Swift?

Nil has no equivalent in Swift.


1 Answers

nil and NULL are essentially the same, nil is something like (NSObject *)0, while NULL is more like (void *)0. But both are pointers with an integer value of zero. You can send messages to nil without raising an error.

NSNull and NULL (or nil, of course) are different things, however. You just use NSNull as a helper to add an empty object to an NSArray or another container class, since you can't add nil to them. So instead, you use [NSNull null] as a replacement, and you have to check if an array element is NSNull, not if it's nil (it will never be equal to nil).

like image 168
MrMage Avatar answered Oct 17 '22 08:10

MrMage