Difference between objective-c and java

55,753

Solution 1

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.

Solution 2

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

Solution 3

Google Java vs Objective c
Here is one that looks pretty good ...
Look at this link http://www.peachpit.com/articles/article.aspx?p=377302

Solution 4

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).

Share:
55,753

Related videos on Youtube

sheLa
Author by

sheLa

Updated on July 09, 2022

Comments

  • sheLa
    sheLa almost 2 years

    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)

    • Nick Craver
      Nick Craver about 14 years
      You're java app isn't subject to rejection at any point including after it's approved for reasons unpublished and subjectively and selectively administered at random...or did you mean the programming languages? :)
    • Remus Rusanu
      Remus Rusanu about 14 years
      Have a look at stackoverflow.com/questions/2641210/…. Is not exactly Java vs. Objective-C, but may be helpful still.
    • Nick Craver
      Nick Craver about 14 years
      I was too tired to use "your" vs "you're" correctly...fail :(
    • cs95
      cs95 almost 5 years
  • TS.xy
    TS.xy about 12 years
    Hi, you said that "•All methods are public", I am very confused about this statement as I thought you could define your private methods in the implementation .m file. Only the methods from .h file are treated as public class.
  • JeremyP
    JeremyP about 12 years
    @TS.xy Yes you can declare methods in the implementation file and that will flag uses outside of the implementation file with a compiler warning, but the code will compile and will run. There's no check at run time for use of "private" methods.
  • Aaron Brager
    Aaron Brager about 11 years
    @JeremyP - you may want to update this post to discuss ARC.
  • Christopher Perry
    Christopher Perry about 10 years
    I prefer IntelliJ to Eclipse, and it's much better than XCode. I'm guessing AppCode would be better for iOS dev