A quick intro to iPhone development for Javans

| | Comments (1) | TrackBacks (0)
A brief Java-oriented glossary
If you are coming from Java (the language, not the place), then here's a very brief Java-to-UIKit guide.  (Note: some of these are only approximately equal)
  • interface (Java) = protocol (ObjC)
  • static method = class method
  • java.lang.Object = NSObject
  • javax.swing.JOptionsPane = UIAlertSheet
  • null = nil
  • System.out.println("my object=" + myObject); = NSLog(@"my object=%@", myObject);
  • "a literal string" = @"a literal string"
  • java.lang.String = NSString
  • boolean = BOOL
  • java.util.ArrayList = NSMutableArray
  • map = dictionary
  • this = self

For loop
One thing that sort of bugs me is that you aren't allowed to do this: "for (int i=0; i<10; i++)"...you can't declare the variable 'i' there.  You have to do this instead:
int i;
for (i=0; i<10; i++)

Methods in ObjC
(Note: the word "method" will be used interchangeably with the words "function" and "procedure".)

The most obvious difference between Java and Objective C is probably the ways methods are declared. 

A regular ole method declaration looks like this:
- (int) getSomeNumber: (int) aParameter withAnotherParameter: (NSString*) aStringParameter
This would be equivalent to:
private int getSomeNumber(int aParameter, String aStringParameter)

  • First, notice that it starts with a hypen (a minus sign).  If this were a plus sign (+), it would mean the function is a "class method" (known in Java as a static method).
  • Next, notice that Objective C puts the variable type in parentheses, sort of like you do when you cast from one type to another.  The parentheses do not enclose the entire parameter list.
  • Notice there are no commas...also note that ObjC uses labels for parameters (starting with the second one).  In the previous example, it is "withAnotherParameter".  This is just something that helps describe what the next variable is, and I could have just as easily used "fooMcLovin" or some other nonsense instead.
  • The asterisk means that ObjC uses the same pointer notation as C/C++.

When you call the function, it will look something like this:
int x = [myObject getSomeNumber: 32 withAnotherParameter: @"some string I made up"];
(Technically, in ObjC, you aren't calling methods...you are sending messages, but we'll ignore this distinction for the time being.)

Other differences
There are many more differences that I'm not going to cover in this article...e.g. header files, pointers, memory management (no more garbage collection).  These will hopefully be addressed in some form or another in later articles.

0 TrackBacks

Listed below are links to blogs that reference this entry: A quick intro to iPhone development for Javans.

TrackBack URL for this entry: http://mauvilasoftware.com/cgi-bin/mt/mt-tb.cgi/7


Craig Pugsley said:

Hi there!

I just wanted to let you know that I found this post amazingly useful. I'm a Java developer developer too - mainly Swing and RCP/SWT/Eclipse-based stuff - but would dearly love to develop for the iPhone. I have a good few ideas, but I'm finding Objective-C quite impenetrable at the moment.

Your pointers guide was extremely useful for someone like myself who just needs someone with experience in both languages to say 'you know how you did it like that in Java, well this is how you do it in Objective-C'.

I urge you, sir, to expand your article in to something more extensive. I'm sure there would be a fair few people like myself who are attracted to coding by the ease of Java, but really hunker after the options and flexibility offered by developing for the iPhone.

Please feel free to drop me an email back, as I'm not sure whether your blog will tell me automatically if you reply to this post.

Thanks again! Great work!

Leave a comment

About this Entry

This page contains a single entry by JLA published on January 26, 2008 8:55 AM.

Programming software for the iPhone was the previous entry in this blog.

Getting started (on Windows) is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.



Powered by Movable Type 4.1-en-release-26-r1120-20071224