Gtk constants in Java
Per Bothner
per@bothner.com
Mon Jul 17 07:13:00 GMT 2000
Maurizio De Cecco <maurizio@mandrakesoft.com> writes:
> Why is [using "enum objects"] an overkill ?
>> After all, you just add one object for each constant, i don't see the
> overhead problem; by the way, the int constant inside the object is
> useless (other than documentation), since then you use object identity
> to check the constant, not object equivalence (i.e. '==').
First, there is an impedance mismatch: The Gtk data structures and
fields use enum values, which are just ints. If the Java API uses
object references, then whenever you cross the Java/C boundary you
have to convert between int and pointer. This is not expensive or
difficult (to convert int to object just use the int as an index
in an array), but it is noticable extra overhead.
> Shouldn't readibility and type safeness more important in this case ?
Perhaps. But Java does not not have have enum types. Instead, Java
uses final static int. It is *less* readable if we try to simulate
enum types using enum objects, since that is not a standard Java idiom.
> Well, if a constant is an object, than you use arrays or collection Sets as OR;
> that is surely more overhead, but ...
>> OK, Sets will be less efficent; about readability, uh, it depends on the point
> of view; for example (imaginary constants and functions):
>> GtkOption[] opts = {GtkOption.8BIT, GtkOption.GREY, GtkOption.SVGA};
>> GtkFrobScreen(opts);
This is *really* bad. Consider checking to see if GtkOption.GREY is
a member of opts. This now requires a loop - and a loop that indexes
through an array. This is very slow. Allocating an array is also very slow.
--
--Per Bothner
per@bothner.com http://www.bothner.com/~per/
More information about the Java
mailing list