Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

nomar383

macrumors 65816
Original poster
Jan 29, 2008
1,310
0
Rexburg, ID
So I've been doing Java Programming now for several months and I feel like I have a pretty good grasp on OO Programming and all of the common terms. I would just like someone to point me in a good direction to learn the syntax differences between Java and Objective-C so I can begin coding for the iphone relatively quickly. Thanks!
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
Okay here's a more complete example.

Java (taken from here):
Code:
public class Bicycle {
	
    // the Bicycle class has three fields
    public int cadence;
    public int gear;
    public int speed;
	
    // the Bicycle class has one constructor
    public Bicycle(int startCadence, int startSpeed, int startGear) {
        gear = startGear;
        cadence = startCadence;
        speed = startSpeed;
    }
	
    // the Bicycle class has four methods
    public void setCadence(int newValue) {
        cadence = newValue;
    }
	
    public void setGear(int newValue) {
        gear = newValue;
    }
	
    public void applyBrake(int decrement) {
        speed -= decrement;
    }
	
    public void speedUp(int increment) {
        speed += increment;
    }
	
}

Objective-C:
Code:
// file Bicycle.h
@interface Bicycle : NSObject {
	int cadence;
	int gear;
	int speed;
}

- (id)initWithCadence:(int)startCadence speed:(int)startSpeed gear:(int)startGear;

- (void)setCadence:(int)newValue;
- (void)setGear:(int)newValue;
- (void)applyBrake:(int)decrement;
- (void)speedUp:(int)increment;

@end


// file Bicycle.m
@implementation Bicycle

- (id)initWithCadence:(int)startCadence speed:(int)startSpeed gear:(int)startGear {
	if (self = [super init])  {
	    gear = startGear;
	    cadence = startCadence;
	    speed = startSpeed;		
	}
	
	return self;
}

- (void)setCadence:(int)newValue {
	cadence = newValue;
}

- (void)setGear:(int)newValue {
	gear = newValue;
}

- (void)applyBrake:(int)decrement {
	speed -= decrement;
}

- (void)speedUp:(int)increment {
	speed += increment;
}

@end

Sample use:
Code:
Bicycle *bike = [[Bicycle alloc] initWithCadence:10 speed:14 gear:3];
[bike applyBrake:8];
[bike release];

If you're not familiar with Cocoa's memory management, it may take a little getting used to, especially if you're used to Java's garbage collection.
 

psingh01

macrumors 68000
Apr 19, 2004
1,571
598
Doesn't Cocoa have garbage collection now? I'm pretty sure that's an option for the iPhone as well.
 

Sayer

macrumors 6502a
Jan 4, 2002
981
0
Austin, TX
Whether it has garbage collection or not, I think I read something on that, but it is prolly under NDA for anyone who downloaded the SDK so I can't really comment publicly.
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
Objective-C 2.0 brings us garbage collection:

http://developer.apple.com/leopard/overview/objectivec2.html

I've yet to get up to speed on Objective-C/Cocoa, so I'm assuming if you're developing new Mac apps with the latest XCode, you get it without having to explicitly enable it?

No it's not enabled by default in Xcode 3. There is a page on Apple.com somewhere that says how GC enabled apps can affect performance by 25%, so that is probably why it isn't on by default.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.