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.
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:
A small learning curve for Java programmers
Fast prototyping of full-featured applications
Developing cutting-edge user interfaces with few lines of code
Using system resources through simple and consistent programming interfaces
The following sections explain the basic steps Java developers need to perform to adopt Objective-C.
Learn the Objective-C Language
Learn the Cocoa Class Hierarchy
Learn Memory Management in Cocoa
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:
Java source files are compiled into bytecode object files and archives that can be used in more than one platform. Objective-C source files are compiled into machine-language object files. These files contain code tailored to a specific platform. Therefore, to build an Objective-C program targeted at more than one platform, each source file must be compiled once for each of the targeted platforms.
C uses header files. Header files declare the interface that a module’s client uses to interact with the corresponding implementation (a set of object files or libraries). The separation of public interface from implementation allows developers to conceive and release the interface to a module before implementing it. Objective-C maintains this separation.
Like C, Objective-C has no automatic garbage collection. You must explicitly release the memory occupied by unused objects. Fortunately, Cocoa provides a mechanism you can use to facilitate this task. See Learn Memory Management in Cocoa for details.
Objective-C is a late-binding language. That is, the compiler doesn’t bind method invocations to the corresponding implementations at compile time. Instead, the Objective-C runtime binds method invocations to the appropriate implementation at runtime. This eliminates the need to cast objects to the type that implements a particular method and allows the development of generic code.
For an in-depth discussion of Objective-C, see The Objective-C 2.0 Programming Language.
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.
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.
© 2002, 2006 Apple Computer, Inc. All Rights Reserved. (Last updated: 2006-10-03)