In this installment of Letters to the Editor, Merlin Hughes helps a former C++ programmer with Java; Mark Johnson answers several questions about his XML series; Bill Venners receives 'one of the best feedback e-mails' ever about his farewell column; and Bill Day fields Java 3D questions Step by Step: ‘Reading textual data: Fun with streams’ by Merlin Hughes Read Porsches, Subarus, and fun with streams Merlin, I jumped from C++ to Java several months ago. A question comes to mind when reading your code: public class UndoReader extends Reader { protected static final int INITIAL_UNDO_SIZE = 64; protected Reader reader; protected int[] buffer; ...... } My question: Reader is an abstract class, that is, it cannot be instantiated. If so, then how can we use it as a data member? Jianming Wang Jianming, All subclasses of Reader — FileReader and InputStreamReader, for example — can be assigned to a generic Reader variable. As an example, we can treat any car implementation (Porsche or Subaru, for example) as a generic car type. The same applies here — it doesn’t matter what a specific Reader is, it still provides the generic Reader API so it can be stored in a Reader variable. Merlin Hughes ‘XML for the absolute beginner’ by Mark Johnson Read Printing files with XML Mark, I read your column this afternoon and found it very educational. I’ve read a few books on XML, and I think it will be a great standardizing tool to make full-scale applications easily accessible from anywhere — from custom, system-dependent GUIs, to Web browsers or even future cellular phones. I’m writing to say I think your article demonstrates, in a rather unexpected way, what XML could do where HTML fails: it allows you to control the placement of your Java code on the page. Indeed, why not? Readers who don’t want to read through the code no longer have to scroll the browser window down page after page and those who want just text can simply scroll the text area. But what about those who want to print such files? They will, of course, just get the top of the code. With XML and XSL, one could define different ways to display the data on a screen or on a printer. That way, you can receive a full code printout on paper. But I guess that will come some day. Nicolas Seigneurin Nicolas, Thanks for writing. I like your idea of using XML to present articles online in one format and print them in another. It would be interesting if a “print preview” function of an application allowed you to select (or customize) the style sheet to be used for printing. Unfortunately, we’re not quite there yet. As for not being able to print the source files when I use TEXTAREA page elements for source code, I’ve had similar complaints from other people. I’ve found a solution: If you look closely at the filename of the source file (usually in the caption), you’ll notice it’s a hyperlink. Click that link, and it will take you to a printable version of the source that’s in the text box. Mark Johnson XML parsers Mark, I enjoyed your article, “XML for the Absolute Beginner.” I’ve been searching for C++ parsers for XML. Do you know of any publicly or commercially available C++ class collections for parsing XML? Stephen Benz Stephen, The best known parser for C, not C++, is Expat (XML Parser Toolkit) by James Clark. This freely available parser is being used to add XML support to Netscape 5 and Perl. It’s highly regarded and is available from: https://www.jclark.com/xml/ IBM has promised XML for C, which I’m interested in having a look at. IBM reps talked about it at XTech ’99 in San Jose, CA, a week and a half ago, but it’s not yet available on alphaWorks: https://www.alphaworks.ibm.com Although most of the parsers on the Internet.com site are written in and for Java, you may find one here that suits your needs: https://stars.com/Software/XML/parsers.html Microsoft apparently had a free XML parser in IE4. Maybe it’ll tell (or sell) you something. A C++ parser, with a MFC-looking API, is among Sam Blackburn’s classes at: https://ourworld.compuserve.com/homepages/sam_blackburn/wfc.htm I don’t know anything about it. You might also check out “Whisper” by Jesse Jones: https://www.halcyon.com/www3/jesjones/Whisper/Home.html Of course, there may be a good reason for the paucity of C/C++ implementations of XML parsers. Maybe Java’s simply a better choice for most (though, of course, not all) applications. Mark Johnson IBM’s XMI proposed standard up to snuff? Mark, I just wanted to say that I really enjoyed your JavaWorld articles on XML technology. We’re using XML fairly extensively on this project as the import/export format between disparate tools. We’re looking forward to the day when a standard DTD is in place for UML. Keep up the great work! Mike Clark Mike, Thanks for writing, Mike. It’s always nice to hear from an enthusiastic reader. If you don’t already know about it, check out: https://www.software.ibm.com/ad/features/xmi.html This is IBM’s home page for XMI, a standard proposed by IBM, Unisys, Oracle, and others for XML representation of UML. It’s yet another of the many metadata proposals in the works. The W3C working group on metadata also met at XTech ’99 last month in San Jose, CA. Hope this information is of use to you. Mark Johnson Design Techniques: ‘Farewell to Design Techniques’ by Bill Venners Read Loose threads? Bill Venners responds Bill, I have enjoyed reading your columns. Your farewell column prompted me to review your August 1998 column, “Designing for thread safety.” While I believe the article is correct in what it says, I’d like to point out some important omissions. The article focuses on JVMs running on single processors. It does not ask the user to consider what happens when Java is executed on a multiprocessor machine, especially one with a relaxed memory model. The Java Memory Model (JMM) was written to accommodate multiprocessors with relaxed memory models. Threads are allowed to cache values, and writes to main memory may be reordered. The JMM is specified in the Java Language Specification (JLS, Chapter 17, “Threads and Locks”). Doug Lea has also written a draft chapter for the next edition of his Concurrent Programming in Java, “Programming to the Java Memory Model”: https://gee.cs.oswego.edu/dl/cpj/jmm.html According to the current spec, including (according to Doug Lea) any revisions that are expected to occur in the near future, being able to atomically access variables does not guarantee the visibility of those variables (visibility between threads, that is, not the ‘scope’ kind of visibility), nor the relative order in which variables appear to change. Without synchronization, writes might not happen for a long time, and when they do, they may appear out of order. In the presence of multiple threads, a shared object reference is a fragile thing. To illustrate, consider the “double-check” idiom for singleton construction: public class Singleton { private static Singleton instance; private Object value; private Singleton() { /* ... */ } public static Singleton getInstance() { if (instance == null) { synchronized (Singleton.class) { if (instance == null) { instance = new Singleton(); } } } return instance; } public static Object getValue() { return getInstance().value; } } One might think that this was a safe, efficient way to construct an immutable singleton object. Not according to the spec! Consider this statement: Object v = Singleton.getValue(); The thread that constructs the singleton may write out the shared reference to the singleton instance before the contents of the singleton are written out. (In this case, “contents” is the reference to value plus the contents of the value object itself.) This means that getValue may see a non-null instance reference, but it may not see the fully constructed contents. The only way to prevent a thread that is accessing the contents of this singleton from seeing transitional values is to synchronize the accessor method on the same lock as the getInstance method. That is, the static getValue method would need to be synchronized too. Better to just use a fully synchronized getInstance method, especially in light of the new fast-locking JVMs. Personally, I think it’s out of character for a language designed for correctness, portability, and security — a language without pointers, with garbage collection, and with runtime checking of array indices and type conversions — to allow inconsistent object views between threads. (Maybe if I point out enough bugs, I can somehow push the JMM toward a more programmer-friendly design.) But I think Java may be stuck between a rock and a hard place here. With processor caches and relaxed memory models in the real world, it may be impractical to specify more consistent behavior for the JVM. (Perhaps a new concurrency mechanism is needed.) In the meantime, I think it’s important for you to help Java programmers understand the implications of the JMM: Document everything that is shared between threads and which locks they depend on, and rely on canned utilities, proven design patterns, and convention whenever possible. Neither the compiler nor the runtime will offer much assistance. Joe Bowbeer Joe, Thanks very much. This is perhaps one of the best feedback e-mails I’ve received. You are right. Having spent a lot of time with my nose immersed in the JVM spec, I am aware of how loose the threads spec is. I always say it’s loose because threading models in the real world are quite diverse, and the JVM thread spec must be loose to maximize the JVM’s portability. Keeping this in mind as you do, multithreaded programming is definitely something I’d like to point out in my book. Bill Venners JavaWorld Exclusive! ‘The Volano Report: Which Java platform is fastest, most scalable?’ by John Neffenger Read Neffenger on Windows 95/98 VolanoMark, Apple’s MRJ John, Wonderful article! I’m curious, why wasn’t Windows 98 included? On another note: I’ve recently read that Apple claims that its JVM is “as fast as Microsoft’s.” Judging from your article, it doesn’t appear that this is something to brag about? Rob Chandler Rob, Windows 98 was not included because it was unable to obtain the high number of socket connections required by VolanoMark. I was able to get only about 40 socket connections using loopback and roughly 80 connections over the network with Windows 95 and Windows 98. To answer your second question, if Apple’s Mac OS Runtime for Java (MRJ) is now as fast as Microsoft’s JVM, it has made a big improvement. I’ve found that the Apple MRJ 2.1 solves a lot of the performance, networking, and AWT problems that previous MRJ releases had. John Neffenger Media Programming: ‘Image processing with Java 2D’ by Bill Day Read Pixel interrogator applet — Bill Day helps out Bill, I’m currently studying for a bachelor of science degree at Kingston University, Surrey, England. As part of my final year project I am trying to write a Java applet that will interrogate every pixel in an image, decide if it is a certain color (blue, for example), and discard it if it is. I am using pixel grabber to grab the entire image into an array, but after that I’m totally stuck! I really would appreciate any help, as deadline dates are fast approaching. Mark Taylor Mark, My main suggestion would be to look closely through the Java 2D API, assuming you have the luxury of using Java 2 for your project. I believe you can use the BufferedImage to access the pixel-by-pixel RGB information to decide if the pixels fall within your desired color range. You can them set them to whatever values you like. Bill Day Media Programming: ‘3D graphics programming in Java: Part 1 and 2, Java 3D’ by Bill Day Read Java 3D programming: The fundamentals Bill, I’m looking for some help with 3D programming in Java, using only what is available in Java 2. I’m having difficulty understanding 3D transformation and rotating objects in 3D. As I am new to Java, I don’t know what objects are available to me in the kit. I’m trying to create two 3D rectangular boxes, which are connected together, and be able to rotate them on the x, y and z axis. I haven’t written any code yet to do this, because I don’t understand the concepts I need to create a 3D object. If you can help me, I would really appreciate it. David Lee David, It sounds like you need to review some of the more fundamental parts of 3D programming in general, then read through the Java 3D Web site, specs, and javadocs in more detail after that. For a general graphics programming book, I recommend Introduction to Computer Graphics by Foley et al: https://www.amazon.com/exec/obidos/ASIN/0201609215/billday/ The Java 3D site is at: https://java.sun.com/products/java-media/3D/ Learning 3D graphics programming can seem difficult and intimidating. I would encourage you to hang in there and to use the book as a reference when you encounter problems in the Java 3D site. I must say that Java 3D is just about the easiest 3D graphics API out there, so it is a good one for beginners to use. Now, for a general Java reference, I would recommend looking around on the Sun Java site: https://java.sun.com The tutorials may be especially helpful to you. Bill Day JavaProgramming Languages