Re: Java and C
Re: Java and C
- Subject: Re: Java and C
- From: Jean-François Veillette <email@hidden>
- Date: Wed, 29 Aug 2001 12:41:56 -0400
Le Mercredi 29 ao{t 2001, ` 11:30, Smith, Bradley a icrit :
>
Java in a Cocoa app is just another programming language. It will be
>
compiled into native PowerPC code so it'll be quicker than a pure Java
>
app.
It is a wrong impression that I've seen more than once.
Cocoa application written in Java are not natively compiled, at least
not the java part.
You can mix Java/ObjC code in a Cocoa app, Objective-C code will be
natively compiled, java code will be compiled as usual in a .class file
(eventualy merged in a .jar file in your app package).
There is a verry cool technologies that was developed for WebObject that
mixed Objective-C and Java for a long time now, it's called the
JavaBridge.
Essentialy it will make every java object to appear as an Objective-C
object when you are writing Objective-C code. It will make every objc
object to appear as a java object when you are writing java code.
There are two way that this is working: morphing and bridging.
Basic objects will be morphed: NSString <-> java.lang.String, NSNumber
<->java.lang.Number, etc...
More complex object will be bridged: NSArray <->
com.apple.foundation.NSArray, etc.
From Objective-C code, the java bridge is transparent, you just use it
(see the documentation on the java bridge for the details).
From Java code, you have to wrapp your objective-C classes before they
can be used on the java side (see java wrapper).
Essentialy when you write a cocoa/java application, you use the bridge,
you have in memory an Objective-C runtime, a JVM runtime and a bridge in
between. Obviously at launch time we now have more things to
initialize, so it take longer. The bridge is pretty optimized by now,
but nonetheless there is a cost to use it, let's say that the cost is
cheap unless you use it too often (in a big loop for example).
NSApplication, the run-loop, NSView is all the same Objective-C
frameworks.
Your java code; a controller, a delegate, a subclass of NSView, is
talking to objective-C object via the bridge.
Is it clearer ?
>
What I want to know is about the garbage collection (GC). Pure Java does
>
auto GC - albeit badly most of the time. how does it work in Cocoa?
The bridge take care of keeping references of java objects when a
objective-c object do a retain, and does a retain on objective-c objects
where there is a java references (and the corresponding release as well).
>
Can we just write our code allocating memory willy-nilly and let the
>
system sort it
>
out?
From the java side, yes.
But be aware, it is not all perfect, sometime to avoid references cycle
in Objective-C some objects won't retain some references, called weak
references (for example delegates, etc.), causing the java bridge to not
keep references of a some java objects. You will have to work around by
having an extra references of your java objects on the java side. If
you don't have that extra reference, the GC will collect it, but the
objective-C object will still point to it.
the java bridge is a well known technology used in WebObjects, you may
look at mailing list archives on webobject to have more useage history.
- jfv