
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Tuesday 02 March 2004 01:32 pm, John Lamb wrote:
Steven T. Hatton wrote:
I'm still not sure if templates will add all that much to Java. I've found that in most cases where templates would be useful, I can usually isolate the type casting to very limited and regular points in the program. Type casting is about the only thing templates seem to eliminate as far as I can see. The negatives of templates seem to outway the positives as far as I can tell. Sure, it takes some getting used to in Java, but once you accept things are as they are, and are willing to work with them, it works quite well.
Simple template functions aren't _that_ useful, but STL stuff is. For example, the STL code to sum all the elements of a dynamically resizeable vector of doubles is reasonably simple:
double total = 0; for( std::vector<double>::iterator i = my_vector.begin(); i != my_vector.end(); ++i ) total += *i;
I never write loops without the {}, except for demonstration purposes. It's too easy to overlook when maintaining code. I guess it's a matter of taste. I found templates to have some hidden costs, such as bloated autogenerated code. They are also a bit tricky when it comes to recursion which is something I am very inclined to use. As I said, the only thing they seem to add to a language is the obviation of type casting. Type casting is not nearly as problematic in Java as in C++, so getting around it doesn't gain us that much. The ability to use the ++ operator comes from operator overloading, not from templates, so templates, in themselves won't get rid of the Iterator.hasNext() and Iterator.next() in the following code: /** Main */ import java.util.ArrayList; import java.util.Iterator; public class Main { public Main() { } public static void main(String[] args) { /* Create the dynamic data array; Java doesn't handle dynamic arrays of primitive types, so we use wrappers. Java does, however, support more flexible creation of fixed size arrays at runtime. */ ArrayList myData = new ArrayList(); for(int i = 0; i < 11; i++){ myData.add(new Double(i)); } double result = 0; /* The Java way: Iterator is common to most Java container types. Enumeration is available in "legacy" types. Note that we have to cast from Object to Double in order to access the Double.doubleValue() method; */ Iterator it = myData.iterator(); while(it.hasNext()){ result += ((Double)it.next()).doubleValue(); } System.out.println("result="+result); /* A C'ish approach. */ result = 0; for(int i = 0; i < myData.size(); i++){ result += ((Double)myData.get(i)).doubleValue(); } System.out.println("result="+result); /* A C++'ish approach. */ result = 0; for(Iterator itr = myData.iterator(); itr.hasNext(); result += ((Double)it.next()).doubleValue()); System.out.println("result="+result); /* If you like using the [] style indices for arrays. */ result = 0; Double[] da = (Double[])myData.toArray(new Double[0]); for(int i = 0; i < da.length; i++){ result += da[i].doubleValue(); } System.out.println("result="+result); } } /*end Main*/
Java has some support for this kind of thing, but not yet quite as good. The thing I don't like is the clunkyness of the code. Python is nicer. APL could do it as my_data?+/my_vector (IIRC)
Mathematica: Plus @@ myData
Java is not always that slow. I don't know what benchmarks have been recorded for Java 3D and OpenGL, but Java3D is pretty snappy. The slow part of Java typically involves the time it takes to load into memory.
Depending on the estimate C++ can run 5-15 times as fast,
I have a genuine mistrust of benchmarks, but I would like to know what sources you have on this. The general philosophy of Java is to write code that is intelligible before even thinking about efficiency. That is not the case with Java3D. In Java3D members are shamelessly exposed as public, and many of the common approaches to passing values are avoided for the sake of efficiency.
though for many GUI apps that's not an issue. The last java app I wrote deliberately slows itself down so you can see it work. What gcj may do is make java compete with C++ on speed also.
Non enim propter gloriam, diuicias aut honores pugnamus set propter libertatem solummodo quam Nemo bonus nisi simul cum vita amittit.
Try pasting the text into Gooogle.
-- JDL
I did, but didn't get any hits. When I take a break, I'll try again. - -- STH Nil Conscire Sibi. Tandem Si. http://baldur.globalsymmetry.com/gs-home/images/ -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.2 (GNU/Linux) iD8DBQFARoPpwX61+IL0QsMRAnhfAKDG29PgqPLnQld3C/bgJjHxdfzV5QCgmtyz yXeqjbU507ARKMxqGbYxpNs= =PbEy -----END PGP SIGNATURE-----