Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > Mac OS X Development Forums > Objective-C, Python, Ruby Development

Reply
 
LinkBack Thread Tools Display Modes
Old 03-26-2011, 11:37 AM   #1 (permalink)
Registered Member
 
Join Date: Aug 2010
Posts: 36
chicagosky is on a distinguished road
Question Comparison with Java

Hello again!

I am a veteran Java programmer and am struggling with a fundamental concept in Obj-C.

In Java, we initialize a class always by calling the new function, for example

Code:
String obj = new String("some string");
But in Obj-C I am noticing that we can directly call methods on a class without initializing a new instance of it, for example

Code:
NSString *obj = [NSString stringWithString:"some string"];
How is this possible without allocating and initializing an instance of the class first?
chicagosky is offline   Reply With Quote
Old 03-26-2011, 12:05 PM   #2 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

The equivalent of your java code would be this. The "alloc" is the equivalent of "new" (it reserves the memory) and the init is the constructor that sets the default values.
Code:
NSString *obj = [[NSString alloc] initWithString:@"some string"];
Some methods - often called "convenience constructors" - return autoreleased obejcts. When you call stringWithString: it DOES call alloc and init, but it will also add the object to the current "autorelease pool" before returning it. You can continue to use that object until the pool is drained (usually at the end of the current event loop iteration) and then it will be destroyed.

Autorelese is a convenient way to create temporary objects that have no "owner" - no one keeping it alive except the autorelease pool. There is no such equivalent in Java, where objects are kept alive until all pointers to it are set to null or fall out of scope.

Cocoa memory management
__________________

Free Games!
smasher is offline   Reply With Quote
Old 03-26-2011, 12:07 PM   #3 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

Actually, sorry, I'm not sure I answered the right question. Are you asking how we can call [NSString anything], since we don't have an instance of NSString yet? That's because alloc and stringWithFormat: are class methods. You have the equivalent in Java, right, with static class methods?

Code:
ClassName.someClassMethodName(args);
In fact you might use a class method to do the same thing in Java without calling "new."
Code:
String obj = String.valueOf(17);
__________________

Free Games!

Last edited by smasher; 03-26-2011 at 12:13 PM.
smasher is offline   Reply With Quote
Old 03-26-2011, 12:29 PM   #4 (permalink)
Registered Member
 
Join Date: Aug 2010
Posts: 36
chicagosky is on a distinguished road
Default

Thanks - you answered both my questions. I'll probably keep this thread alive with future questions and I suspect there are many Java programmers out there who share some of the same questions that I do.
chicagosky is offline   Reply With Quote
Old 03-27-2011, 11:38 AM   #5 (permalink)
Registered Member
 
Join Date: Aug 2010
Posts: 36
chicagosky is on a distinguished road
Default

I apologize in advance for this very elementary question but the following piece of code confused me

Code:
NSRange range = [someString rangeOfString:@"some"];
Isn't NSRange a class? Shouldn't we declare a pointer first, then instantiate an object before calling methods on it? I mean if the above is valid then why do we do the following?

Code:
NSString *someString = @"some string";
chicagosky is offline   Reply With Quote
Old 03-27-2011, 04:15 PM   #6 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

Not everything that starts with "NS" is a class. NSRange and NSInteger, for example, are not.

NSRange is a struct; a struct is just a collection of fields, kind of like an object without methods. NSInteger is a kind of int, a primitive type. I know Java has primitive types, but I don't think it has structs.

If you're in doubt you can always control-click a type like NSInteger and it will jump to the definition. Or command-click? I'm not at an apple keyboard. Whichever one is "jump to definition." Eventually you'll just know which are which and you won't have to look it up.

Structs and functions are features of the C programming language, which Objective-C is built on top of. Accessing a struct or a function is faster than accessing an object property or a method, so you still see them poke through in some places.
__________________

Free Games!

Last edited by smasher; 03-27-2011 at 10:14 PM.
smasher is offline   Reply With Quote
Old 03-27-2011, 08:49 PM   #7 (permalink)
Registered Member
 
Join Date: Aug 2010
Posts: 36
chicagosky is on a distinguished road
Default

Thank you very much - this helps a lot. Some of these types definitely are very different from Java and porting my mind from there is taking some time. Thanks for your help - it is much appreciated.
chicagosky is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 481
16 members and 465 guests
7twenty7, AlanFloyd, AppsBlogger, David-T, HemiMG, iAppDeveloper, imac74, Jaxen66, lovoyl, Music Man, mutantskin, Paul Slocum, SLIC, solardrift, unicornleo, usernametaken
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,683
Threads: 94,131
Posts: 402,932
Top Poster: BrianSlick (7,990)
Welcome to our newest member, unicornleo
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 10:14 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0