-4

What's the basic difference between the two..? it would be nice if can someone explain using the example of NSInteger and NSNumber.. Thanks

Community
  • 1
  • 1
Ankit Srivastava
  • 12,347
  • 11
  • 63
  • 115

5 Answers5

10

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.

aleroot
  • 71,077
  • 30
  • 176
  • 213
  • 1
    Not main difference and in some cases not true at all.. NSInteger *i = malloc(sizeof(NSInteger)); NSString *constString = @"constString"; - quite opposite to what you said. – Oleg Trakhman Jun 14 '12 at 11:09
  • Of course you can force a value type to stay on the heap, but it is not the default behavior ... In your example you are manually allocating memory and declaring a pointer to an NSInteger , you are not using the value type, but the pointer to access a memory address ... – aleroot Jun 14 '12 at 11:13
  • 1) Memory type is still not the main difference of objects and Plain old data types. 2) In many languages for example C++ objects can be allocated in stack. And it's default behavior there. 3) In some cases Objective-C objects can be allocated in stack. – Oleg Trakhman Jun 14 '12 at 11:32
  • This is not a particularly useful distinction to draw. A primitive-typed ivar will be on the heap, with the object that holds it. – jscs Jun 14 '12 at 19:23
8

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.

Wubao Li
  • 1,728
  • 10
  • 13
2

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];
2

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.

0

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.

Abhishek
  • 2,255
  • 1
  • 13
  • 21