< Previous PageNext Page > Hide TOC

Legacy Documentclose button

Important: The Java API for Cocoa is deprecated in Mac OS X version 10.4 and later. You should use the Objective-C API instead. For a tutorial on using Cocoa with Objective-C, see Cocoa Application Tutorial.

Adopting Objective-C

Objective-C is a powerful, yet simple programming language. It’s as simple as C but with the advantages that object orientation, dynamic typing, and late binding provide. Cocoa is a technology that facilitates the creation of powerful and innovative applications with relatively little custom code. Cocoa allows programmers to be very productive by implementing many of the user-interface behaviors that Mac OS X users expect in their applications. It also provides easy-to-use programming interfaces to system-level resources, such as memory, files, and interapplication-communication facilities. Cocoa uses Objective-C as its native language to take advantage of its speed, object orientation, and extensibility.

This document introduced you to Cocoa using Java, a language familiar to you. However, to create applications that can take advantage of all the benefits Cocoa provides, you must implement your applications using Objective-C.

In many ways, Objective-C and Java are very similar: They are both object-oriented languages with a C-based syntax. Although moving from Java to Objective-C presents a few challenges, the rewards are many, including:

The following sections explain the basic steps Java developers need to perform to adopt Objective-C.

In this section:

Learn the Objective-C Language
Learn the Cocoa Class Hierarchy
Learn Memory Management in Cocoa


Learn the Objective-C Language

The Objective-C language is based on the C language, which has a very simple syntax. The Java language borrows heavily from C’s syntax; therefore, it should be easy for Java programmers to familiarize themselves with the syntax used in Objective-C.

These are some of the salient differences between Java and Objective-C:

For an in-depth discussion of Objective-C, see The Objective-C 2.0 Programming Language.

Learn the Cocoa Class Hierarchy

Just as Java programmers must learn the hierarchy of several Java packages—such as java.lang, java.util, and java.awt to develop applications—aspiring Cocoa programmers must familiarize themselves with the Foundation and Application Kit (AppKit) frameworks, Cocoa’s main frameworks.

Cocoa applications use the Foundation framework to work with essential data types, such as strings, numbers, and dates, and to interact with system resources. They use the AppKit framework to present their user interface to the user and respond to user events, such as button clicks and menu choices.

For more information about the Cocoa frameworks, see The Cocoa Frameworks in Cocoa Fundamentals Guide.

Learn Memory Management in Cocoa

Garbage collection in Java frees programmers from having to release the memory used by objects that are not referenced by other parts of a running application. However, in general, programmers have little control over the garbage collector. Although the garbage collector attempts to perform its duties during an application’s idle time, sometimes its activities may adversely impact an application’s performance. Certain Java virtual machines, such as HotSpot, contain very efficient garbage collectors and may allow programmers or system administrators to configure them for specific usage patterns. But this approach might negate to some level the advantages of worry-free object instantiation.

The Objective-C language doesn’t have a garbage collector. But Cocoa uses a mechanism based on reference counting to determine when it’s safe to free the memory occupied by an object no longer in use. This mechanism, however, requires programmer intervention. When you create an object or receive an object from a routine that creates it but otherwise doesn’t “own” it and you want the object to persist for an arbitrary amount of time, you tell Cocoa that you want to hold on to (or retain) the object. When you’re done with the object, you tell Cocoa that you want to let go of (or release) the object. Through these messages, Cocoa increments or decrements a counter that indicates how many references to the object exist. When the reference count reaches zero, Cocoa disposes of the object and releases the memory it occupies. The advantage of this approach over Java’s garbage collection is that programmers have more control over when objects are disposed of.

For details on memory management in Cocoa, see Memory Management Programming Guide for Cocoa.



< Previous PageNext Page > Hide TOC


© 2002, 2006 Apple Computer, Inc. All Rights Reserved. (Last updated: 2006-10-03)


Did this document help you?
Yes: Tell us what works for you.
It’s good, but: Report typos, inaccuracies, and so forth.
It wasn’t helpful: Tell us what would have helped.