Java to iPhone Syntax Differences

Discussion in 'Mac Programming' started by nomar383, Mar 10, 2008.

  1. macrumors 65816

    nomar383

    Joined:
    Jan 29, 2008
    Location:
    Rexburg, ID
    #1
    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!
     
  2. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
  3. thread starter macrumors 65816

    nomar383

    Joined:
    Jan 29, 2008
    Location:
    Rexburg, ID
    #3
    That's definitely a helpful start, thanks! Anyone else have basic syntax equations between the two?
     
  4. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #4
    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.
     
  5. thread starter macrumors 65816

    nomar383

    Joined:
    Jan 29, 2008
    Location:
    Rexburg, ID
    #5
    Wow, this is an awesome example, thanks a lot :)
     
  6. macrumors 65816

    Joined:
    Apr 19, 2004
    #6
    Doesn't Cocoa have garbage collection now? I'm pretty sure that's an option for the iPhone as well.
     
  7. macrumors 6502a

    Sayer

    Joined:
    Jan 4, 2002
    Location:
    Austin, TX
    #7
    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.
     
  8. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #8
    No, it doesn't.
     
  9. macrumors 65816

    Joined:
    Apr 19, 2004
  10. macrumors member

    Jeff Hall

    Joined:
    Apr 10, 2006
    #10
  11. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #11
    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.
     

Share This Page