Objects in Objective C

Discussion in 'Mac Programming' started by luke71933, Apr 7, 2014.

  1. luke71933 macrumors member

    Joined:
    Aug 17, 2012
    #1
    Hi,
    Why are NSString and NSNumber etc considered as objects in objective C ? For ex. in java int and String are not objects they are just saying the data type of the variable. I am following the code school try objective c course and it is a bit confusing.

    Thankyou
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    In Java, an int isn't an object, but a String definitely is. In Java, you also have the Number type, which is an object type, and is the abstract base class of Long, Integer, and other classes.

    In Objective-C, you have the int type, which is not an object.

    Or maybe you confused Java with JavaScript. They are two different languages, with different types for int and String than either Java or Objective-C.


    If you're having trouble with a particular thing on a specific website, it's best to post the exact URL where you're having trouble. That way we can all see the same page.
     
  3. subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #3
    It's because they are objects and responds to methods and so on. If you look up the Apple documentation on these classes you can see the methods they have. You can for example ask a string object how long it is, a primitive type is just a "dumb" container, ie some byte storage of a specific size. But Objective C also have access to the primitive types that it shares with C such as int, float etc.
     
  4. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #4
    In Obj-C NSString is an object, but char[] is a primitive. Similarly, in Java String is an object but char[] is a primitive (I'm pretty sure, not certain, but pretty sure, that arrays of primitives in Java are primitives themselves).

    In Obj-C NSNumber is an object, but int is a primitive. Similarly, in Java Integer is an object but int is a primitive.

    If you want some actually confusing things, an NSInteger in Obj-C is a primitive, not an object like you'd expect from the name patten. In Java, float is a primitive but Float is an object.

    In general, languages with both objects and primitives make all kinds of arbitrary and confusing distinctions between them that you'll just have to learn. C doesn't have such confusing distinctions, because it lacks objects. Python doesn't have such confusing distinctions because it lacks primitives. Racket doesn't have such confusing distinctions because it's abstracted and optimized to the point that you're never sure what you're dealing with, but it always seems to just do the right thing despite the fact it regularly changes how it's storing data at runtime. (I think Obj-C might do a similar thing, masking the class changing behind class clusters. That's why NSArrays are rarely actually NSArrays at runtime. But that's enough of an adventure down the rabbit hole for now).
     
  5. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #5
    Nope. All Java arrays are objects.

    Array objects might have odd-looking names, but they respond to every method defined for the java.lang.Object class. For example, you can ask a byte-array for its Class object, ask it for toString(), and so on.

    The array-subscripting that fetches and stores elements in the array are not done by methods. Refer to a tutorial on Java byte-codes for the details of how arrays are accessed.
     
  6. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #6
    Hm, I thought that there were special primitives called int[ (no close) and float[ and so on... I feel like that detail is normally hidden except when you use reflection on them (which I haven't needed to do in two years).

    Although you're talking about JBC so you probably know better than I...
     
  7. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #7
    http://docs.oracle.com/javase/specs/jls/se7/html/jls-10.html
    First line: "In the Java programming language, arrays are objects". What happens in byte code is of interest, but isn't actually that relevant as far as the language spec is concerned. Different compilers can target different JVMs and generate different byte code. I imagine they're often similar, but it doesn't affect the details of the language.

    The OP hasn't returned, but there seems to be a disconnect somewhere in what they've learned. Java hides pointers, so nothing is adorned with a star. There's also the weird idea of boxed primitives, and some oddities like int.class, which is different from Integer.class. In Objective-C some of the machinations are laid bare because of its C heritage, so pointers abound, but then some #defines and a mix of structs and objects can certainly muddy what's what. Names that look object-y are #defined to primitives (often per-platform, so you get the right thing), some structs have a type defined for a reference to them so you really have a pointer without the *, etc. It's a mixed bag, and it's all, IMO, tied to the C underpinnings. That's not bad, and being a proper superset of C has advantages, but it can make you cock your head when you see something that looks wrong. Also, the dot notation to access properties just muddies things further, IMO.

    Anyway... It's all great. I love every quirk and nuance. It gives us something to think about and fodder to argue the nuances.

    -Lee
     

Share This Page