Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between objective-c and java [closed]

Tags:

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)

like image 717
sheLa Avatar asked Jun 06 '10 01:06

sheLa


People also ask

Is Objective-C same as Java?

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.

Is Objective-C close to C++?

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.

Is Objective-C faster than Java?

Yes. C++ executes faster that Java in most cases.

Does Apple use Objective-C?

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.


2 Answers

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:

  • the syntax is different
  • Memory management is more manual. On the iPhone, you have to use retain/release (OS X has garbage collection). This is not actually as bad as it sounds. If you follow the rules, and wrap your instance variables in getters and setters you'll find yourself rarely having to write retain or release. Update: some time after I wrote this, Apple introduced automatic reference counting (ARC). ARC grew out of the observation that the clang static analyser was capable of spotting just about every single missing (or extra) retain or release. So they extended the principle by having the compiler put in the retains and releases automatically. Apart from some simple rules about strong and weak relationships (i.e. whether an object claims to own another object or not), you can more or less forget about memory management. Also, ARC is available on iOS.
  • All methods are public. This is a direct consequence of the message sending paradigm, but you can't define private or protected methods.
  • The library is much smaller. In particular, you will notice that there are only three collection classes NSArray, NSDictionary and NSSet (plus their mutable versions). The philosophy is that you program to the interface. The runtime worries about what the implementation should be.

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.

like image 57
JeremyP Avatar answered Oct 19 '22 02:10

JeremyP


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"!

like image 37
strange quark Avatar answered Oct 19 '22 02:10

strange quark