问题
What's the basic difference between the two..? it would be nice if can someone explain using the example of NSInteger and NSNumber.. Thanks
回答1:
The main difference is related to where they stay in the memory, objects are stored in the heap while value type are stored directly in the Stack ...
heap : is an area of memory used for dynamic memory allocation.
stack : is the section of memory that is allocated for automatic variables within functions. Data is stored in stack using the Last In First Out (LIFO) method.
About NSInteger and NSNumber :
NSInteger is nothing more than a synonym for a long integer, while NSNumber is an Objective-C class, a subclass of NSValue to be specific.
回答2:
object is : member data + function operating on the data
so, primitive data type is just data, no method directly related to it.
object is something like a module, include the data and function (method here).
NSInteger is primitive data type. NSNumber is object, it's member data maybe NSInteger.
回答3:
Primitive data types store a direct value, for example NSInteger stores an integer value (either a 32 bit Integer, or a 64 bit Integer depending on the compiled architecture), Objects are an instance of a class, with methods, properties, etc.
In order to get an NSInteger from an NSNumber, you would use
[aNumber intValue];
回答4:
Primitive data types are used for storing fundamental types of data, such as strings, integers, and real numbers. So when you declare a value type variable, the compiler sets aside, or allocates, a chunk of memory that is big enough for that variable. The way reference types work is different. When you are working with a reference type, you are using two things, an object that is created in memory and a variable that references to the object. The variable does not hold an actual piece of data with which your program will work. Instead it holds a special value known as a reference, which links the variable to the object.
回答5:
Primitive data type is just a data while object type is know as reference type.
Which is a class which have two behavior data member and member function.
来源:https://stackoverflow.com/questions/11031732/what-is-the-difference-between-primitive-data-type-vs-an-non-primitive-data-type