The Caboteria / Tech Web / TechNotes > ProgrammingNotes / JavaNotes (09 Mar 2015, TobyCabot)
(over to JavaProgrammingBookmarks)

Java is a popular programming language written by Sun Microsystems. It's an object-oriented language that runs inside a "virtual machine" which, at least in theory, allows code to run on many different machines. Sun calls this "Write once, run anywhere", and in practice it actually works reasonably well, although it took years for this to happen.

Java is extremely popular in commercial software development - it's dominant in the area that I work in: operations support software for communications companies. It's not that popular in the Free Software community because it's a proprietary product owned and controlled by Sun. There is a lot of "open source" development in Java, though.

The Java home page is http://java.sun.com/; you can download a developer kit or run-time from there. If you're running on Linux and don't need the latest-and-greatest API then you can use the IBM JVM which I've found to be faster than Sun's: http://www6.software.ibm.com/dl/lxdk/lxdk-p

Java per se is a proprietary product, but there are many free projects that aim to provide all (or part) of the Java environment as Free Software. http://www.dwheeler.com/java-imp.html lists many of those projects.

See also: JavaProgrammingBookmarks, SwingNotes, JHotDrawNotes, GeronimoNotes, PrevaylerNotes, JbossTips

Books

http://www.mindview.net/Books/TIJ/ - Bruce Eckels Thinking in Java. I used Thinking in C++ to learn C++ and it was excellent, so it's likely that this book is, too.

O'Reilly Nutshell Book. Nice to have, although I find that I use javadoc more and the book less these days.

Development Tools

building: http://ant.apache.org/ - "like make only without make's wrinkles." I can't remember the last time I worked on (or looked at) a Java project that built using make.

unit test: http://www.junit.org/ - "keep the light green to keep the code clean." I'm definitely test-infected, writing and running unit tests is becoming second nature.

modelling: argouml - a UML modelling tool that can generate Java skeletons.

debugging: jswat

database access: http://www.squirrelsql.org/ - good, not great, database browser in Swing.

Java Development Environment for EMACS - http://jdee.sf.net/

A Source Measurement Suite for Java - http://www.kclee.com/clemens/java/javancss/

Infrastructure

Java application servers are available from a bunch of commercial vendors like Sun, IBM, BEA, Oracle, etc but there are some compelling open source options, too. They tend to work well, and the price is right, but the documentation is often weak. See JavaProgrammingBookmarks#EJB_Containers.

Servlet: Tomcat http://jakarta.apache.org/tomcat/ is the reference implementation of Servlets and JSP. I've used it standalone and integrated with JBoss and it's pretty sweet. Back in the 3.x days it was slower than Jetty but I've heard that 5.x is comparable.

MVC: At some point Sun declared that Smalltalk's "Model View Controller" was the right way to implement web user interfaces. Without getting into the discussion of what constitutes a "model" in a stateless environment, there are many frameworks to help you build web applications. http://jakarta.apache.org/struts/ Struts is by far the most popular, and is the only one I've seen with enough market share to become a resume buzzword. Many people aren't fond of Struts, which is one reason why there are so many me-too application frameworks. I'm fond of one called Maverick http://mav.sf.net/ which is very simple and elegant. It doesn't include a lot of what Struts does (form processing, validation, tag libraries, etc) but its basic flow is very cool - a simple XML file describes pipelines that start with a DOM-ified representation of Java objects and pass through multiple processing stages. It works well with XSLT.

Coding Conventions

Use Sun's http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html and override what you feel is wrong. Personally, I feel that the 80 column constraint is too limiting; I prefer to allow people to use 132 column line widths.

When something is an identifier, and you feel that you must append "Id" in some form to the name, use ID not Id. It's easier to type and reflects that most people call it an "eye-dee" not an "id".

When a class has a member variable that holds multiple items, don't make a setFoo(Collection foos) method, make an addFoo(Foo foo) method instead. Maintain the collection (or set or whatever) internally. The difference is that there's some type checking in the second approach whereas the first approach lets the client pass a Collection full of anything in. It's also often more convenient for the client to call add() a few times rather than create their collection, fill it, and then set() it. If they want to look at it you can return either a collection/set/list (some interface) or you can return an array or iterator of some sort.

Misc

Here's a cute hack to enable token substitution in java property files: http://www.sys-con.com/java/source.cfm?id=1228

Using Java on Debian GNU/Linux

When I tried to run Sun's JDK 1.3.1 on Debian Sid (in March 2002) I got the following error: /home/tcabot/local/Linux/jdk/bin/i386/native_threads/java: error while loading shared libraries: libstdc++-libc6.1-1.so.2: cannot open shared object file: No such file or directory. This can be solved by linking to the existing libstdc++, i.e. ln -s /usr/lib/libstdc++-libc6.2-2.so.3 /usr/lib/libstdc++-libc6.1-1.so.2.

Java is a proprietary language (controlled by Sun) but there are a few Free implementations of the compiler, JVM, and class libraries. If you're running a new version of Debian you can apt-get gcj and kaffe.

$ gcj -C HelloWorldApp.java
$ kaffe -addclasspath . HelloWorldApp

Java is very resource-intensive: cpu, memory, and processes. So you might bump into the limits that Unix uses to limit individual user resource consumption. An important one is max user processes which you can see if you run ulimit -a. You probably want to bump this up to 1020 or so: ulimit -u 1020. Other limits that you might bump into are SHMMAX and SHMANY which you can set using files in /proc/sys/kernel/ or by setting values in /etc/sysctl.conf.

IBM JVM on Linux

IBM's JVM 1.4.1 for GNU/Linux doesn't seem to recognize old-style timezone names. My /etc/localtime was a link to /usr/share/zoneinfo/US/Eastern and the JVM would never use daylight savings - it was always GMT+5. When I changed the link to /usr/share/zoneinfo/America/New_York it worked fine. See http://www.mainframeforum.com/t590240.html which seems to be relevant despite being a mainframe forum.

public class date { public static void main(String[] argv) throws java.io.IOException {System.out.println(new java.util.Date()); }}

Make sure that /etc/sysconfig/clock is correct. The ZONE value should point to a valid file in /usr/share/zoneinfo.

Arrays vs Collections

Why don't people use arrays in Java? It seems to me that people get so excited about Collection, Set, List etc that they forget about arrays. Arrays have some definite issues, but they also have some advantages. Arrays are fixed-length so they're not good if you're not sure how many things that you're going to put into them in advance. Their big advantage (which limits their use) is that they're strongly typed. This can be an advantage in many cases, i.e. where you've got a bunch of the same thing. In this case an array is nice because it enforces type safety in a way that a Collection doesn't.

In summary, an array is the correct data structure to use where you have a known number of objects all of the same type.

Float sucks

Binary arithmetic (e.g. Java float) doesn't cut it when applied to money - it's not accurate on the right side of the decimal point. Java seems to have the answer in java.math.BigDecimal but from what I've seen on the web it's not a very good implementation.

IBM has proposed a replacement of the BigDecimal class, the website is at http://www2.hursley.ibm.com/decimalj/. The site does a great job of explaining why decimal arithmetic is better than binary and also has many interesting links. They also have an implementation available but it's under a very restrictive license, you can only use it for 90 days and then you must destroy it - there's no way to buy it, either.

The IBM page points to a commercial implementation of a BigDecimal class at http://users.belgacombusiness.net/arci/math/. It's not very expensive - 500 Euro (which is closer to $500 than those common-market pinkos would hope!). It claims to be fast and have good formatting features.

There was another implementation but it seems to have fallen off the net. Google had cached entries for a package called TCE/Java at http://www.voicenet.com/~hsk0/tce/ but it seems to have fallen off the net. The guy that wrote it is Howard Kapustein, maybe he'll show up again.

Update - it's not just a Java problem, since it's inherent to binary representations of fractions. In fact, people have died because of this problem: http://www.ima.umn.edu/~arnold/disasters/patriot.html

XML Event-based Parsing

http://cafeconleche.org/books/xmljava/ - looks like a good starter book.

Java as of version 1.4 comes with a competent XML parser and XSLT transformer built in, but sometimes it's preferable to use a different one, for features or speed.

There are two ways that an application can get a parser so you need to either know which one you're trying to use or cover both.

SAX Parser

This is the old SAX 1.0 approach.

Property

Code

http://java.sun.com/j2se/1.4.2/docs/api/javax/xml/parsers/SAXParserFactory.html

XML Reader

This is the SAX 2.0 approach.

Property

Code

http://java.sun.com/j2se/1.4.2/docs/api/org/xml/sax/helpers/XMLReaderFactory.html

            parser = XMLReaderFactory.createXMLReader();

STaX

A "pull" approach where the application requests events from the parser. Need to check it out further. Intro at http://www.xml.com/pub/a/2003/09/17/stax.html .

Profiling

There are many expensive commercial tools for profiling Java programs while they're running, but since Java has the JVMPI interface pretty much anyone can hook into the JVM at run time and find out what's going on. This is another category of programs where there seem to be far too many half-baked efforts, but here are my notes from a few hours of fooling around with various tools.

Mike's Profiler - http://mjp.sourceforge.net/ looks promising, has actual documentation and the maintainer seems to be maintaining it on an ongoing basis. The GUI is easy to understand. Unfortunately it seems to run very slowly and crash before JBoss can fully boot up. NOTE - I need to test 0.06 which has a statistical sampling mode

JTreeProfiler - http://sourceforge.net/projects/jcoverage/ this one streams to an XML file on a per method invocation basis so it's probably unsuitable for anything but trivial profiling (booting JBoss wrote a 2.3GB file). The analysis GUI is interesting and very graphical but hard to make sense of.

JPerfAnal - http://sourceforge.net/projects/jperfanal/ can you think of a more unfortunate name for a performance analysis tool? In any case this guy seems to have the right idea in that he reads the stock Sun profiler output so he doesn't need a platform-specific library to gather data. Unfortunately his GUI is lame and there's not one word of documentation.

Extensible Java Profiler - http://ejp.sourceforge.net/

JMP - http://www.khelekore.org/jmp/ GTK GUI, written in C.

JRat - http://jrat.sourceforge.net/ takes an interesting approach - you "instrument" your code after you build.

GCViewer - http://www.tagtraum.com/gcviewer.html a GUI that visualises the output of the JVM GC dump info.

PTDS - http://bocks.psych.purdue.edu/~pizman/PTDS/

Debugging

JSwat's not bad: http://jswat.sourceforge.net/

How to connect a debugger to a running JVM: http://www.ncsa.uiuc.edu/UserInfo/Resources/Hardware/IBMp690/IBM/usr/idebug/help/en_US/debug390/tasks/tb3attjv.htm

How to run a JVM in debug mode: http://java.sun.com/products/jpda/doc/conninv.html - e.g. -agentlib:jdwp=transport=dt_socket,server=y,address=8000

Edit | Attach | Print version | History: r36 < r35 < r34 < r33 < r32 | Backlinks | Raw View | Raw edit | More topic actions
Tech.JavaNotes moved from Tech.JavaTips on 02 Jul 2004 - 19:51 by Main.guest - put it back
Copyright © 2008-2017 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding The Caboteria? Send feedback