Compare Objective-C syntax with Java syntax

Hello World

  • Objective-C
1
2
3
4
#include <Foundation/Foundation.h>
NSLog(@"Hello World");
NSLog(@"myInt = %li", (long)myInt);
NSLog(@"This is %@", str);
  • Java
1
2
3
4
5
import java.io.*;
import java.util.*;
System.out.println("Hello World");
System.out.println("myInt = " + Integer.tostring(myInt));
System.out.println("This is " + str);

Class

Objective-C

1
2
3
4
5
6
7
@interface Fraction : NSObject
@property int denominator, numerator;
-(void) print;
-(int) numerator;
-(double) convertToNum;
-(void) setTo: (int) n over: (int) d;
@end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#import "Fraction.h"
@implementation Fraction
@synthesize numerator, denominator;
-(void) print {
NSLog(@"%i/%i", numerator, denominator);
}
-(double) convertToNum {
if (denominator == 0) {
return NAN;
}
else
return (double) numerator/denominator;
}
-(void) setTo:(int)n over:(int)d {
numerator = n;
denominator = d;
}
1
2
3
Fraction *myFraction = [[Fraction alloc] init];
[myFraction setTo:2 over:3];
[myFraction print];
  • Java
1
2
3
4
5
6
7
8
9
10
class Fraction {
int numerator, denominator;
void print() {
System.out.println(Integer.tostring(numerator) + "/" + Integer.tostring(denominator));
}
void setTo(int n, int d) {
numerator = n;
denominator = d;
}
}
1
2
3
Fraction myFraction = new Fraction();
myFraction.setTo(2, 3);
myFraction.print();

Functions

  • Objective-C
1
2
3
4
5
6
7
8
-(double) convertToNum: (Fraction *) f
{
if (f.denominator != 0) {
return (double) f.numerator/f.denominator;
}
else
return NAN;
}
  • Java
1
2
3
4
5
6
7
double convertToNum(Fraction f) {
if (f.denominator != 0) {
return
}
else
return null;
}

Collections

Array

  • Objective-C
1
2
3
4
5
6
7
8
9
10
11
12
NSArray *monthNames = [NSArray arrayWithObjects: @"January", @"February", @"March", @"April", @"May", @"June", @"July", @"August", @"September", @"October", @"November", @"December", nil]; 
// To mark the end of the list, nil must be specified as the last value in the list; it is not actually stored inside the array.
// a easy way to set up array: @[@"January", @"February", @"March", @"April", @"May", @"June", @"July", @"August", @"September", @"October", @"November", @"December"], don't need nil.
for (int i = 0; i < 12; ++i) {
NSLog(@" %2i %@", i+1, [monthNames objectAtIndex:i]);
// array[index] is allowed, as well as array[index] = object
}
// another shorter for loop
for (NSString* currentString in monthNames)
{
NSLog(currentString);
}
  • Java
1
2
3
4
String[] toppings = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; 
for(int i = 0; i < 12; i++) {
System.out.prinln(Integer.tostring(i) + monthNames[i]);
}
  • Objective-C
1
2
3
4
5
6
7
8
NSMutableArray *numbers = [NSMutableArray array];
// mutableArray
for (int i = 0; i < 10; ++i )
numbers[i] = @(i); // [numbers addObject: @(i)]; append
for (int i = 0; i < 10; ++i )
NSLog (@"%@", numbers[i]);
// fast enumeration, %@ can used at array
NSLog (@"%@", numbers);
  • Java
1
2
3
4
5
int[] numbers;
for (int i = 0; i < 10; i++)
number[i] = i;
for (int i = 0; i < 10; i++)
System.out.println(number[i]);

Dictionary

  • Objective-C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// Dictionary
NSMutableDictionary *glossary = [NSMutableDictionary dictionary];
[glossary setObject: @"A class defined so other classes can inherent from it" forKey: @"abstract class"];
[glossary setObject: @"To implement all the methods defined in a protocol" forKey: @"adopt"];
glossary[@"archiving"] = @"Storing an object for later use"; // another way to use dictionary
NSLog(@"abstract class is %@", [glossary objectForKey: @"abstract class"]);
NSLog (@"adopt: %@", glossary[@"adopt"]);
NSLog (@"archiving: %@", glossary[@"archiving"]);
// Enumerating a dictionary
NSDictionary *glossary2 =
[NSDictionary dictionaryWithObjectsAndKeys:
@"A class defined so other classes can inherit from it",
@"abstract class",
@"To implement all the methods defined in a protocol", @"adopt",
@"Storing an object for later use",
@"archiving",
nil
];
// Print all key-value pairs from the dictionary
for ( NSString *key in glossary2 )
NSLog (@"%@: %@", key, [glossary2 objectForKey: key]);
NSDictionary *glossary3 = @{
@"abstract class" :
@"A class defined so other classes can inherit from it",
@"adopt" :
@"To implement all the methods defined in a protocol",
@"archiving" :
@"Storing an object for later use"
};
  • Java
1
2
3
4
5
6
7
8
9
10
11
12
13
Map<String, String> glossary = new HashMap<String, String>();
glossary.put("abstract class", "A class defined so other classes can inherit from it");
Map<String, String> glossary2 = new HashMap<String, String>{
"abstract class" :
"A class defined so other classes can inherit from it",
"adopt" :
"To implement all the methods defined in a protocol",
"archiving" :
"Storing an object for later use"
};
for (Map.Entry entry : glossary.entrySet()) {
System.out.println(entry.getKey() + ", " + entry.getValue());
}

Reference

Programming in Objective-C 6 Edition