Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are some of the different names of terms and concepts in Objective C as compared with Java?

I was hoping to make a list of some of the differences in terms between java and objective c to abate my confusion. It makes explaining my difficulties easier when I know what things are called.

For example:

  • null - nil
  • this - self
  • map - dictionary (not even quite sure about this one)

What I'm looking for are similar comparisons or corrections to ones I have listed. Not just limited to elements in the language, but concepts as well...

like image 276
user149100 Avatar asked Aug 01 '09 22:08

user149100


2 Answers

You're right about map = dictionary. I'll add:

  • @public, @private, @protected (the default) are for instance variables only (not methods) and work like C++'s visibility modifiers (i.e. you declare them as section headers rather than before each variable).
  • Class methods are like static methods in Java
  • There are only two data structures: NSDictionary and NSArray (in immutable and mutable variants). These are highly optimized and work well for most situations. For everything else, there's CHDataStructures
  • @interface doesn't work like Java's interfaces - it defines the instance variables and methods of a single class.
  • You need header files. C is to blame for this. This pretty much sucks, as maintaining the things is an unnecessary pain.
  • There is no concept of a "package". The closest thing is a framework, but these should not be used as Java's packages (i.e. don't create them just to organize your classes).
  • Instead of "new Class()", say [[Class alloc] init]. Think of "alloc" like the new operator and init as the constructor.
  • id's are generic object pointers, like references of type Object in Java.
  • The base class is NSObject. Inheritance from NSObject is not automatic and must be explicitly specified.
like image 111
Ryan Ballantyne Avatar answered Oct 19 '22 14:10

Ryan Ballantyne


Another conceptual difference is the behavior when calling methods (sending messages) on null objects (to nil).

Java

MyClass myObject = null;
myObject.doSomething(); <-- results in a NullPointerException

Obj-C

id myObject = nil;
[myObject doSomething]; <-- is valid

Here is a very good SO question about that behavior.

like image 44
Thomas Zoechling Avatar answered Oct 19 '22 14:10

Thomas Zoechling