Objective-C vs. Java?
Asked by
zpok (
5)
October 2nd, 2007
Syntax and concepts explained by example?
Observing members:
0
Composing members:
0
4 Answers
I’m fairly new to objective-C but have a lot of java experience. Java is a bit more heavyweight because of the JVM, but that overhead is negligible in modern computers. Objective-C seems to be a more elegant language in that it is more closely related to smalltalk than java. The typing in objective-C can be static or dynamic (if you use the ‘id’ type) which is a “best of both worlds” system. I’ve found java’s facilities for dynamic method invocation more trouble than it’s worth.
Method calls in objective-C are more human friendly in the end because the method signatures allow you to label the parameters. Method calls in obj-c are different than the usual C/C++/Java methods but I think that once you get used to them it is much nicer. For example in java you might have a method call on a string:
someString.indexOf(“x”, 5);
which shows the first instance of “x” at an index greater than or equal to 5. In objective-c this would be something like:
[ someString indexOf:“x” fromIndex:5 ]
I think the objective-c version is much more readable.
Objective-C is a superset of C which means it can be very fast and is compatible with a lot of low-level libraries.
I also like the fact that invoking a method (or “sending a message” in obj-c terms) on nil/null just returns nil instead of throwing an exception. It means your code doesn’t have to be littered with if(xyz!=null) statements.
Another benefit of objective-C is that you can “forward” method calls to other objects and easily refer to methods as first class values that can be passed around. I don’t think that this is possible in java.
What I really like about java is the truly cross-platform VM and GUI (swing) library. If you avoid using platform-specific libraries then you can be pretty sure your java code will run anywhere that has a decent JVM (which is just about everywhere). Another benefit of java is that it is virtually impossible to write server code that is vulnerable to buffer overflows and other exploits that are common in software written in C and its derivatives.
In general, even though I’m still an objective-C newbie I prefer it to java. But I make my living working primarily with java because all of my work projects (so far) have had to be cross platform.
For more information on objective-C and Cocoa, I strongly recommend Cocoa Programming for Mac OS X by Aaron Hillegas. The first few chapters explain the fundamentals of objective-C in a clear and friendly way.
Thanks for the insights. Gratifying to read that you as a Java programmer see advantages in Obj-C. Having no C background, I am a bit overwhelmed and tend to see the differences between Java and Obj-C as awkward. Which is unfair of course…
I like Apple’s high level documentation, eg for design patterns, but am looking for a better primer on the nitty gritty, the so-called simple things.
Do you know of a comprehensive comparison from a Java point of view?
The Hillegass book is actually a pretty good starter coming from Java. The author usually describes Objective-C features in terms of their Java equivalent, when there is one. When there isn’t a comparable feature in Java he is as clear as any description I’ve seen. I should say that I had several false starts when learning Objective-C but after reading Hillegass’ book I’m actually getting stuff done in the language.
Answer this question
This question is in the General Section. Responses must be helpful and on-topic.