[4466] in java-interest

home help back first fref pref prev next nref lref last post

questions about java

daemon@ATHENA.MIT.EDU (Jon Rosen)
Sat Dec 30 04:02:00 1995

From: Jon Rosen <jfr@bluerose.com>
Date: Tue, 26 Dec 95 20:13:24 -0800
To: java-interest@java.Eng.Sun.COM

I just completed reading the new book Java! (Ritchey) and I have  
several questions about the language in general.  I subscribed to  
the java-interest mailing list, but the subscribe info said that  
the mailing lists are being deemphasized and subscribers are  
encouraged to read comp.lang.java.  Our news access has been  
fritzed for a while and we are working on getting it back in  
shape, so until then, I can only post through the mailing list.   
Please feel free to respond by e-mail to me directly at  
jfr@bluerose.com.

Background: I am a skilled Objective-C developer and a  
semi-skilled C++ developer.  Java appears to be more like Obj-C in  
its simplicity and functionality than to C++ (which is, to me,  
like the proverbial kitchen sink without a drain :-).  I consider  
that goodness (although I will miss the nice Obj-C syntax compared  
to the function-like C++/Java syntax, sigh).

Question 1: The book is unclear as to whether the language is  
indeed more dynamically than statically bound, and whether there  
is enough access to the underlying run-time or to "system" methods  
so that dynamic creation of "classes" (i.e. objects) where the  
actual class is not known until run-time can be achieved.

For instance, in Obj-C, I can create a new object for a given  
class by issuing the following statement:

	MyClass newObject = [[MyClass alloc] init];

This is roughly equivalent to the Java syntax:

	MyClass newObject = new MyClass();

However, assume that I have a string array (myClassName) with the  
name of the class in it at run-time.  In Obj-C, I can use that  
name to instantiate an object of that class despite the fact that  
I don't know what class of object will be allocated when I write  
the code.  I do this as follows:

	id newObject = [[objc_getClass(myClassName) alloc] init];

The function objc_getClass returns a class object for the class  
named in the string myClassName.  The class object can then be  
used to instantiate a new object by sending it the alloc message,  
after which the init message is sent as above. 


(Note: If the class name is not found at run-time, the  
objc_getClass function returns "nil".  In Obj-C, when a "nil"  
object is sent a message, it automatically returns "nil" also, so  
nesting of "failing" code can be achieved without causing a  
run-time error.  It would be better to assign the class object to  
a separate variable and see if it is "nil" first.)

I realize that Java does away with function calls, but this  
functionality could just as easily be implemented in a System or  
base Object class as methods:

	Object newObject = new Object(myClassName);

The constructor for Object with a string parameter could, for  
instance, be used to actually create an object of the named type.

So the question is, does this capability exist in Java or not?  It  
is extremely useful in building dynamic applications, where the  
class that is going to be instantiated is derived from something  
in the data.

A common example of this is in database "glue" programming where  
you want to instantiate objects based on rows returned from the  
database.  NEXT's DBKit created a database "model" in which the  
classes that might be created from a user query were modelled in  
regular text.  The DBKit "glue" recognized which class was being  
created from a given query at run-time and automatically  
instantiated a list of objects of the appropriate type from the  
data being returned from the query.

Another use of this in NEXTStep is the archiving mechanism which  
permits whole networks of objects to be written to disk in a  
psuedo-text format and then at a later time, reinstantiated by  
reading the archive file and creating objects from classes  
dynamically.

The actual functionality has to be provided somewhere in Java so  
the only question is whether the programmer has access to it  
through one of the class libraries or the language?

Question 2: Is it possible to determine whether an object responds  
to a given message at run-time?  This is useful when heterogeneous  
lists or arrays or tables of objects are created, using a base  
Object type as the "handle" to each object (since all objects are  
derived from Object).  Assume, for instance, that I have an object  
called aList which is of class List which contains a list or set  
of objects, none of whose actual classes are known.  In Obj-C, I  
can send a message to each object in the list as follows:

	int index;
	int imax = [aList count];  (returns the number of objects)
	for (index = 0; index < imax; index++) {
		id anObject = [aList objectAt:index];
		if ([anObject respondsTo:@selector(printIt)]) {
			[anObject printIt];
		}
	}

Notice that if I merely attempt to send all the objects in aList  
the printIt message, and one of them doesn't implement the  
message, a run-time error occurs:

	[aList makeObjectsPerform:@selector(printIt)];

This can cause an error unless you know that all the objects are  
homogeneous with respect to the method "printIt".

Is something similar available in Java?

I am very impressed with the garbage collection capabilities.   
This is a defect in Obj-C, caused, of course by the fact that  
Obj-C, like C++, supports not only objects but also direct  
pointers.  Since it is impossible for the run-time system to know  
about "under-the-covers" memory accesses via pointers in Obj-C, it  
is unsafe to implement a true multi-threaded garbage collector.   
Java solves this neatly by getting rid of pointers entirely (and  
good riddance, IMHO).  Pointers were there in order to simplify  
things for programmers in languages which were not "good enough".   
True object-oriented coding simplifies things even more and  
completely obviates the need for pointers.  This is a lesson we  
learned in Obj-C (if you are using a pointer, you can always do a  
better job by creating a class and encapsulating the behavior).   
Unfortunately, the Obj-C language is still based on C, and the C  
remnants are there for the using, even if it is unnecessary.

Another good idea was getting rid of the goto statement.  The only  
problem I see is getting access to a useful parser generator like  
yacc that will support Java.  Yacc-generated code is so tied  
together with goto's that I doubt anyone could make yacc support  
Java directly, even with a good post-processor.  Is there an  
external parser generator (or better yet, a parser class) already  
available for Java?

That about wraps this one up.  Sorry if this stuff is too basic  
for this newsgroup and mailing list.

Thanks for any help I receive in advance.

Jon Rosen
jfr@bluerose.com

-
This message was sent to the java-interest mailing list
Info: send 'help' to java-interest-request@java.sun.com

home help back first fref pref prev next nref lref last post