Objective C – A translation from Java
Hello Objective C (objc) from a (mainly) Java developer. I’d love to welcome you into my portfolio of programming languages but hey, what’s with that slang, that weird terminology?!
In this blog post I will try to provide you with a translation, a dictionary or an index of terms that are used by objc programmers. If I have the time I will even keep it up to date…
I hope I am not breaking any NDAs by posting this, I am sure I will be told if I do…
Let’s not waste any time and jump straight in:
| Java |
Objective C |
What the?! |
| - | id | Just remember: everything is an id. Any object, any … anything. |
| Class (concept) (defined by a class) .class Files use .java extension. |
Class (concept) (defined by an interface and an implementation, see class structure below) class method Interface files use the .h extension, implementation files use the .m extension. |
Wanna know the class? do a [myObject class] |
| Interface | Protocol Files use the .h extension. |
One adopts a protocol rather than implement an interface. |
| this, super | self, super | Easy. |
| null | nil | lol |
| Field | Property (and instance variable) | One declares an instance variable (optional), then declares the @Property and finally specifies how it is implemented (@synthesize, @dynamic). Easy as. |
| Call / Invoke myObject.init() |
Send Message [myObject init] or myObject.init() (quite unconventional though) |
That is how one uses a type’s methods. This gets more complicated, look up Selectors in the context of sending messages. |
| Subclass class A extends BaseClass |
Subclass, Category, Extension @implementation A : BaseClass (CategoryOMG ) |
You see, things are not as simply as you thought. A class can not only be extended but also When defining a category you must import the class’s interface file. |
| abstract | - | N/A |
| public, protected, private | same | After specifying @public (or another) you can just list all the instance variables that have the same visibility. No need to do it for each variable. Cool eh?
One mention goes to package which cares about how many bits your platform has. google for this (search box available above) |
| static method, non-static method static void doSmth(){} void printName(){} |
class method, instance method + (void) doSmth; - (void) printName; |
That is just the method declaration in the objc column. The implementation uses brackets after the method name to group the method’s code, just like Java. |
| public void setOrigin(int x, int y); | - (void) setX:(int)x andY:(int)y;
Default return type is id. |
AndY is just a label for the second argument. Note that the colons and the labels are part of the method name. |
| Object | NSObject | This isn’t necessarily the Class that everything extends from. There is also NSProxy and you can also write your own. Feeling brave already? |
| toString() | description | The NSObject implementation returns the name & address of the object. Sounds familiar? |
| import | #import | One can reference just the class not include the whole thing. See comment below. |
| Class structure: import java.lang.*; class MyClass extends BaseClass implements Comma,Separated,List,OfInterfaces { //method definitions |
A class requires an Interface and an Implementation.
#import “BaseClass.h” @interface MyClass : BaseClass <Protocol1, Protocol2> //method declarations //and then don’t forget the implementation #import “MyClass.h” @implementation MyClass |
Too much can be said and surely you will be asking me questions anyway.
Still: @Class is used when no instances are created / used in your implementation but rather parameter names are of that particular type. Is is simply a low-weight #import. |
| Interface
interface A extends AnotherInterface, YetAnotherInterface{ |
Protocol Specified using @Protocol @Protocol A <AnotherProtocol, YetAnotherProtocol> //required by default @optional @end |
So much better…
Can incorporate another protocol just like Interfaces can extend other interfaces.
Methods are @required (by default) or @optional. Use these to group the mandatory and the optional methods or your You can declare objects that adopt a protocol: And last, you can forward reference a protocol: |
| MyClass myObject = new MyClass(); | MyClass *myObject = [[MyClass alloc] init];
or for non static typing: |
These are virtually equivalent. I suggest you use static typing if you want your compiler to be able to tell you if some method does not exist in the type that you are using, for example. |
| object’s fields initialization { [...] } and for static fields /* Honestly, when did you last use this rather than a constructor with arguments? */ |
- init - initWithBlah - init… and for Class initialization + (void) initialize; |
NSObject’s init returns self. Therefore all classes should define an init method to initialize their instance variables.
The runtime will call initialize before using a class object. I am a little bit confused myself but I believe them. |
| java.lang java.util etc | NS, CG, CF | NS = NextStep CG = Core Graphics CF = Core Foundation unless I am wrong of course. |
| instanceof | Defined in NSObject:
isMemberOfClass – actual class |
Cool!
Responds to selector means that the method exists in the class passed as argument while conforms to protocol means that ALL the methods in the protocol. I suspect that a YES response is returned if the methods defined by the protocol exist in the type passed as argument even if the type does not conform to the protocol. |
| synchronized | @synchronized() | No synchronized keyword for methods. A mutex is required. Self is a possible candidate, another good one can be _cmd as long as the method name is unique. Who is _cmd? Google in the search box above. |
| Exception | NSException
@try; @catch; @finally and @throw are the keywords. |
You can throw other objects, not necessarily part of the NSException inheritance tree.
Once an exception is caught it can be re-thrown by using @throw without having to specify the argument. |
| !!!
Invoking non-existent methods. Say they have been deprecated and removed from the API. |
forwarding
- (void) forwardInvocation: (NSInvocation *) theInvocation { Here’s what just happened: a method that does not exist on our “self” was called and the runtime forwarded the call to the method I wrote above. It could’ve freaked out but it didn’t, it kept its calm and got my kudos. The original Note: the NSObject’s forwardInvocation doesn’t do much. Soz. |
This is so cool, it should be a mandatory feature in all modern programming languages.
Can replace @deprecated, multiple inheritance (I can almost hear your cry), surrogate objects, proxies, delegates and more. It is up to you to confuse the crap out of all those that try to steal your job. Piece of advice: consider overriding respondsToSelector, isKindOfClass, etc to make it look like your surrogate objects actually belong in the type hierarchy. That will simply make you irreplaceable. |
| Remoting | RPC | Remote Messaging
Type qualifiers: Example: |
Normally all these qualifiers are to be used in the protocol that defines the remote messaging, not in the classes that you implement. They are implementation guidelines more than anything.
New to pointers? Then trust me: out and inout only make sense for pointers. |
| @deprecated | @interface SadClass - sadmethod __attribute__((deprecated)); @end |
|
| Spring Framework, Log4j, Maven, Axis, Acegi | LOL, ROFL, ZOMG | I just thought it would be funny |
My conclusion: objc is too powerful for its own good. God have mercy of all those who end up stuck in teams that have bad developers. Just stop and imagine someone abusing Categories and Extensions, what a disaster that project can become. Let me know when that happens though, I may have the right medicine
Do you want to study objc in depth? Well if you have an apple dev account then you already know where to find it. Otherwise try this wikibook.
Cheers…
p.s. special thanks go to my surgeon (who is truly awesome btw) who provided me with the time and the painkillers required to read the objc documentation
By Nick | 30. Sep 2008 | Apple, Java, Objective C, Tutorial | 5 Comments »

This was really useful. A HelloWorld side by side would be even better.
Objective C makes me want to die, but I have to use it… so thanks for this comparison.
Very very nice. Do you plan on keeping this up-to-date? If so I’ll link it.
Wow I am actually the only reply to your incredible post?
Thank you for your comments. I will try to keep it up to date, but I would like to know what else you’d like to see in this “thread”.