i'm experienced with Java and want to learn objective-c to write apps for the iPhone. What are some fundamental differences? (other than syntax)
Objective-C is a compiled OO programming language. Java is both compiled and interpreted and therefore does not offer the same run-time performance as Objective-C. Objective-C features efficient, transparent Distributed Objects. Java features a less efficient and less transparent Remote Machine Interface.
While they are both rooted in C, they are two completely different languages. A major difference is that Objective-C is focused on runtime-decisions for dispatching and heavily depends on its runtime library to handle inheritance and polymorphism, while in C++ the focus usually lies on static, compile time, decisions.
Yes. C++ executes faster that Java in most cases.
And, since then, Objective-C has been the primary language for software at Apple. It's important to note here that Objective-C is a proprietary language, which means that only Apple can make core changes to the language.
Conceptually, the biggest difference is that Objective-C is dynamically typed and you don't call methods, you send messages. This means that the Objective-C runtime does not care what type your object is, only whether it will respond to the messages you send it. This in turn means that you could (for example) create a class with an objectForIndex: method and use it in place of an NSArray as long as the code that uses it only calls objectForIndex:
This allows you to do all sorts of funky things, like have one object pose as an object of a different class and you can add methods at run time or add collections of methods (called categories) to prebuilt classes like NSString at compile time. Most of the time you'll never bother with any of those tricks, except the categories.
On a more practical level you'll notice:
ETA: I forgot one important thing, you'll miss from Java. Objective-C does not support name spaces. This is why you'll see OBjective-C classes with two (or more) letter prefixes and it's the feature I really wish they would add.
First, Objective-C doesn't provide a garbage collector for iPhone. On the Mac, a garbage collector is present.
But, Possibly the biggest difference for me is that there are 2 files for each class. A header file (.h) where you have to declare instance variables, properties, and methods. Then is the implementation (.m) file where you write your methods. Properties in Objective-C have to be "synthesized" with the @synthesize keyword to create the getter and setter methods.
The transition isn't too bad. Both languages follow similar rules in terms of object models and even some of the syntax. I actually made the opposite transition. I started with Objective-C for iPhone, then picked up Java to do Android development.
On an unrelated note, building your UI is much easier using Apple's tools. Interface builder is drop-dead simple. Hooking up UI objects in the nib files to their declarations in code is so easy. Instruments provides an easy way to check CPU usage, memory leaks, allocations, and so on. Plus, just in terms of features, overall polish, and ease of use, I'll take XCode and Apple's tools to Eclipse any day.
If you're "fluent" in Java, the move to Objective-C won't be too hard. Just get your [] keys ready and practice typing "release"!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With