Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Mar 10, 2008, 12:24 PM   #1
nomar383
macrumors 65816
 
nomar383's Avatar
 
Join Date: Jan 2008
Location: Rexburg, ID
Send a message via AIM to nomar383
Java to iPhone Syntax Differences

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!
__________________
iPhone Apps:
LDS Message of the Day, RoadRage, and more...
iPad Apps:
SABCommand (SABnzbd+ Front-End)
nomar383 is offline   0 Reply With Quote
Old Mar 10, 2008, 12:56 PM   #2
kainjow
Moderator emeritus
 
kainjow's Avatar
 
Join Date: Jun 2000
See my post here.
kainjow is offline   0 Reply With Quote
Old Mar 10, 2008, 01:17 PM   #3
nomar383
Thread Starter
macrumors 65816
 
nomar383's Avatar
 
Join Date: Jan 2008
Location: Rexburg, ID
Send a message via AIM to nomar383
Quote:
Originally Posted by kainjow View Post
See my post here.
That's definitely a helpful start, thanks! Anyone else have basic syntax equations between the two?
__________________
iPhone Apps:
LDS Message of the Day, RoadRage, and more...
iPad Apps:
SABCommand (SABnzbd+ Front-End)
nomar383 is offline   0 Reply With Quote
Old Mar 10, 2008, 01:20 PM   #4
kainjow
Moderator emeritus
 
kainjow's Avatar
 
Join Date: Jun 2000
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.
kainjow is offline   0 Reply With Quote
Old Mar 10, 2008, 01:31 PM   #5
nomar383
Thread Starter
macrumors 65816
 
nomar383's Avatar
 
Join Date: Jan 2008
Location: Rexburg, ID
Send a message via AIM to nomar383
Wow, this is an awesome example, thanks a lot
__________________
iPhone Apps:
LDS Message of the Day, RoadRage, and more...
iPad Apps:
SABCommand (SABnzbd+ Front-End)
nomar383 is offline   0 Reply With Quote
Old Mar 10, 2008, 04:39 PM   #6
psingh01
macrumors 65816
 
Join Date: Apr 2004
Doesn't Cocoa have garbage collection now? I'm pretty sure that's an option for the iPhone as well.
psingh01 is offline   0 Reply With Quote
Old Mar 10, 2008, 04:59 PM   #7
Sayer
macrumors 6502a
 
Sayer's Avatar
 
Join Date: Jan 2002
Location: 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.
__________________
Obama is a true statesman whose experience as a state senator, half-term US Senator & guest lecturer in a Constitutional Law class has fully prepared him to take control of our nuclear arsenal.-Me
Sayer is offline   0 Reply With Quote
Old Mar 10, 2008, 05:11 PM   #8
kainjow
Moderator emeritus
 
kainjow's Avatar
 
Join Date: Jun 2000
Quote:
Originally Posted by psingh01 View Post
Doesn't Cocoa have garbage collection now? I'm pretty sure that's an option for the iPhone as well.
No, it doesn't.
kainjow is offline   0 Reply With Quote
Old Mar 10, 2008, 05:29 PM   #9
psingh01
macrumors 65816
 
Join Date: Apr 2004
Quote:
Originally Posted by kainjow View Post
psingh01 is offline   0 Reply With Quote
Old Mar 16, 2008, 05:00 PM   #10
Jeff Hall
macrumors member
 
Join Date: Apr 2006
Objective-C 2.0 brings us garbage collection:

http://developer.apple.com/leopard/o...jectivec2.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?
Jeff Hall is offline   0 Reply With Quote
Old Mar 16, 2008, 07:29 PM   #11
kainjow
Moderator emeritus
 
kainjow's Avatar
 
Join Date: Jun 2000
Quote:
Originally Posted by Jeff Hall View Post
Objective-C 2.0 brings us garbage collection:

http://developer.apple.com/leopard/o...jectivec2.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.

Last edited by kainjow; Mar 16, 2008 at 07:51 PM.
kainjow is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Differences between iPhone 4 and 5s? heycal iPhone 104 May 16, 2014 06:12 PM
Chrome on OS X -- Java plugin / Java 7 vs Java 6 Issue booyahbooyah Mac Applications and Mac App Store 4 May 31, 2013 10:13 AM
Differences in iPhone 5 displays dre2000 iPhone 2 Oct 25, 2012 09:02 PM
iPhone Speed Differences pk7 iPhone 7 Oct 9, 2012 12:55 PM
iPhone 5 differences? honeygirl iPhone 21 Oct 1, 2012 07:55 AM

Forum Jump

All times are GMT -5. The time now is 07:07 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC