PDA

View Full Version : Java to iPhone Syntax Differences




nomar383
Mar 10, 2008, 11:24 AM
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
Mar 10, 2008, 11:56 AM
See my post here (http://forums.macrumors.com/showpost.php?p=5129224&postcount=21).

nomar383
Mar 10, 2008, 12:17 PM
See my post here (http://forums.macrumors.com/showpost.php?p=5129224&postcount=21).

That's definitely a helpful start, thanks! Anyone else have basic syntax equations between the two?

kainjow
Mar 10, 2008, 12:20 PM
Okay here's a more complete example.

Java (taken from here (http://java.sun.com/docs/books/tutorial/java/javaOO/classes.html)):
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:
// 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:
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.

nomar383
Mar 10, 2008, 12:31 PM
Wow, this is an awesome example, thanks a lot :)

psingh01
Mar 10, 2008, 03:39 PM
Doesn't Cocoa have garbage collection now? I'm pretty sure that's an option for the iPhone as well.

Sayer
Mar 10, 2008, 03:59 PM
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
Mar 10, 2008, 04:11 PM
Doesn't Cocoa have garbage collection now? I'm pretty sure that's an option for the iPhone as well.

No, it doesn't. (http://forums.macrumors.com/showpost.php?p=5132798&postcount=20)

psingh01
Mar 10, 2008, 04:29 PM
No, it doesn't. (http://forums.macrumors.com/showpost.php?p=5132798&postcount=20)

:mad:

Jeff Hall
Mar 16, 2008, 04:00 PM
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?

kainjow
Mar 16, 2008, 06:29 PM
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.