开发者

Difference between objective-c and java [closed]

开发者 https://www.devze.com 2023-01-02 16:16 出处:网络
Closed. This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this
Closed. This question needs to be more focused. It is not currently accepting answers.

Want to improve this question? Update the question so it focuses on one problem only by editing this post.

Closed 9 years ago.

Improve this question

i'm experienced with Java and want to learn objective-c to write apps for the iPhone. What are some fundamental differences? (other tha开发者_Go百科n syntax)


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.


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


The biggest difference that will affect you immediately, besides an entirely different set of libraries[1], is that Objective-C doesn't provide garbage collector. The Apple libraries provide some garbage collection related routines and objects, I believe using reference counting, but you don't have the garbage collection you're used to in Java.

Other than that, many things will be similar: single inheritance, late binding, etc. Objective C doesn't provide method overloading, but that's a somewhat trivial difference. Java and Objective-C aren't too far apart in terms of how their object model works. Obj. C has a few tricks up its sleeve, such as categories, but you don't need to worry about those at first.

See the related C# question suggested by Remus for more (and much more detailed) information (and thanks to Remus for reminding me of the library difference - I nearly forgot that important facet).


Any object declared in Objective C is a pointer of another

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号