Cool project idea Dougal
Most of the advice you've been given is great. Watch out for the String == thing... the reason that...
Code: Select all
foo = "hello";
foo2 = "hello";
if (foo == foo2) System.out.println("foo and foo2 are equal");
... works is because the Java compiler recognises the string literals are the same and re-uses the same object again. Use string1.equals(string2) to be sure. Be aware that Strings use TONS of memory (24 bytes before you even put anything in it) so think about using char[] instead where you can.
I'd advise buying a book on Java... seems like a silly outlay but they're usually a good thing to read for 30 mins before beddy-byes every night and quicker than Google for reference purposes most of the time.
For what you're doing, it's not so much of a consideration but, in general, Java memory usage sucks so beware of creating objects all the time, especially if you care about performance. Modern JVMs do a cracking job of escape analysis (determining that an object is only used within a single method call and binning it immediately, saving the garbage collector loads of time) but still... if you can re-use objects, do it. Many Java evangelists claim that the garbage collector is soooo good, but in some cases, it's a pain.
On this note... Java 1.5+ does auto-boxing for you (converting Integer to int) but that will result in object creation. Try to stick to primitives where possible. If you want a list of ints and are forced to use Integer in the Collections classes, try writing your own version that uses the primitive.
If you think of a useful class and find yourself thinking "hey, that's a cool, generic utility type thing" then check to see if it already exists in the standard libraries. You'll be surprised how much is in there and how easy it can make your job (but DO check the library's source code to make sure it's not a performance hog... if it is, subclass it).
Use static inner classes for simple, class-local things... it'll avoid code clutter. Be aware that non-static inner classes use more memory than static ones.
Avoid synchronization unless you know for a fact you need it. It's amazing how much can be achieved through use of the members of the
java.util.concurrent package (e.g. AtomicInteger), especially for keeping counters that are accessed by multiple threads.
Learn how to hand-code Swing... it's the best thing about Java
Reflection is also one of the best things about Java. It's worth learning and isn't as slow (for most things) as many people say.
Get your code working before optimising too much. I know this is a general saying for most languages but the JVM is soooo clever nowadays that sometimes premature optimisation makes your programs run slower rather than faster.
Always, always use the -server option when running java.exe. It can double performance for maths-intensive apps, sometimes more. Recent JVMs detect whether your box has 2Gb+ and 2+ cores and will use -server automatically but bung it on the command line just to be sure. I've found BEA JRockit is the fastest JVM for maths-heavy stuff but it might very well be architecture/configuration dependent.
That was all just a brain dump... there will be loads more, and better qualified wisdom in books on the subject
I personally use Netbeans for my IDE. It's come on a lot since v4.0 but it's very much a preference thing. In particular, the profiler is bloody excellent for finding memory leaks and sussing out performance bottlenecks. Eclipse is also mighty fine.
Ian.