Objective-C for Java Programmers, Part 1
- Language Philosophies
- Objects and Primitives
- Files and Compilation Units
- Object Models
- Static Behavior
- Different Syntax
- Summary
Objective-C was created back in 1986, but was a very niche language for much of this time. The main backer was NeXT, a company that shipped only 50,000 computers over the course of a decade, limiting the language’s exposure considerably. When Apple bought NeXT, this started to change. Objective-C became the primary language for development on the Mac, giving it somewhere up to around 5-10 percent of the desktop application development market share. Objective-C wasn't the only supported development language on OS X though, and a lot of developers used things that they were more familiar with. The iPhone, in contrast, did not support anything other than Objective-C for third-party development.
If you're coming to either platform from a Java background, then you might find the change daunting. Objective-C looks very different to Java. Fortunately, once you get past the syntax, the languages are quite similar, and you'll find that the transition is not as difficult as the syntax might imply.
A number of the Java designers had experience with Objective-C, including some who had worked with NeXT on the OpenStep specification. A lot of ideas in Java are lifted directly from Objective-C, or taken from Smalltalk, which inspired both languages. That's not to say that everything is the same in Objective-C. There are some important differences, which I'll explore in this and the next article.
Language Philosophies
Objective-C was designed to bring the encapsulation support that Smalltalk enjoyed to the C language. One of its designers described it as a hybrid language, with the square bracket syntax as a sign indicating the transition from C code to 'object land.'
The goal of Java was to make a language that was usable by average programmers. This combined Smalltalk-like semantics with C++ syntax. The latter decision was made more for marketing reasons than technical ones. C++ had a large market share (and still does, for some strange reason), and it was easier to make developers switch to a language that looked similar than one that looked different.
This, unfortunately, is the cause of some of the major problems for people switching to or from Java development. Java looks like C++, but behaves a lot more like Objective-C. Both Java and Objective-C have Smalltalk-like semantics, while C++ adds Simula-like semantics to C. Going between Java and C++ involves switching between Smalltalk and Simula semantics, without any corresponding change in the syntax.
Going between C++ and Objective-C is easier; a change in syntax accompanies a change in semantics. Going between Objective-C and Java is a little bit confusing. There is a large change in syntax but only a small change in the semantics.
The difference in goals is obvious in a few places. Java aims to be vaguely C-like and easy for C or C++ programmers to learn, but doesn't mind breaking things when the C way of doing things is not ideal. Objective-C is a pure superset of C. Every valid C program is a valid Objective-C program. One of the main design goals for Objective-C was to produce a language for parceling up C libraries into easily reusable components.
Objective-C originally didn't come with much of a standard library. You were expected to use it with C libraries. The OpenStep specification, designed by Sun and NeXT, is commonly used as a standard library for Objective-C now, with implementation such as Cocoa or GNUstep available on most platforms. Java didn't make it easy to reuse existing code, so it needed a comprehensive standard library from the start.