Official announcement of Java and the GPL

Michael Torrie torriem at chem.byu.edu
Mon Nov 13 21:06:20 MST 2006


On Mon, 2006-11-13 at 19:31 -0800, Ross Werner wrote:
> On Mon, 13 Nov 2006 11:18:47 -0800, Bryan Sant <bryan.sant at gmail.com>  
> wrote:
> > :-( good point.  Using annonymous inner-classes works just like a
> > delegate, but it's more crufty code, and would throw people off when
> > porting their code from C# to Java.  I believe that delegates and
> > closures are being added to Java 7.  Maybe this project would have to
> > be developed in concert with that release of Java.
> 
> I get the general idea of delegates, but not the specifics. Would somebody  
> mind showing some identical-ish C# and Java code, one using delegates and  
> the other using anonymous inner classes?

I found this on
http://www.scratchprojects.com/2006/05/intro_to_gtk_p04.php :

"Delegates are an elegant means to do old school C-style function
pointers. Like function pointers, the actual implementation isn't
discovered until run-time. The only thing that the delegate knows is the
signature of the function. In the example about, C# doesn't care what
happens or doesn't happen in the body of the function. It only matters
that the signature matches what is expected."

So basically a function or method is also a first-class object that can
be passed by reference in its own right.  They are safer than function
pointers because the signatures are checked at runtime.

In Java, since methods or functions never exist on their own, you have
to wrap such a method in an anonymous inner class which can then be
passed to the function binding the event with the event handler.

Observe the following two programs, both in gtk:
C#: http://www.gotmono.com/docs/gnome/bindings/gtk-
sharp/buttonwidgetnormal.html (see example 2/3 way down)

Java: http://java-gnome.sourceforge.net/docs/GNOME-tutorial/x704.html

Notice in C# to add a callback to an event, we use the following syntax:
/* Connect the "clicked" signal of the button to our callback */
button.Clicked += new EventHandler (callback);

In Java it is:
checkButton1.addListener(new ToggleListener() {
        public void toggleEvent(ToggleEvent event) {
                if (checkButton1.getState())
                        checkButton3.setSensitive(false);
                else
                        checkButton3.setSensitive(true);
        }
});

Note how the Java syntax is a bit more awkward.  The good news is that
if you use the right tools you need never worry about this stuff.  In
GTK, for example, you can use Glade to build the gui and then use Glade
classes in C# or Java to build the gui and automatically set up the even
callbacks.  Most people build Swing guis using a GUI builder which sets
up all the listener classes for you.

> 
> Also, while we're on the topic of C# and Java, what makes C# generics  
> "real generics", and Java's generics not really real? Is it because Java's  
> generics are backwards-compatible with non-genericized code, and therefore  
> non-generic-compliant code only generates warnings rather than compile  
> errors?

I hope Bryan will clarify my comments and also answer this question on
generics, since my understanding of it all is not so great.

Michael


> 
> 	~ Ross
> 
> /*
> PLUG: http://plug.org, #utah on irc.freenode.net
> Unsubscribe: http://plug.org/mailman/options/plug
> Don't fear the penguin.
> */
> 




More information about the PLUG mailing list