/ Markus Amersdorfer:home / university / about:me /
\ capsaicin - my blog on growing chilis \


Some Hints about Java

... some hints here on the great programming language Java.

Table of Contents

Get Java

Documentation, Literature and Miscellaneous Topics

Integrated Development Environment (IDE)

One hell of an IDE is the Open Source Project Eclipse.
Give it a try, you'll love it!

You might want to check out "Callisto" (an effort of the eclipse foundation to synchronise software releases of their products, including the IDE).

What's New in Eclipse 3.2 Java Development Tools.

Hints

If you have a directory with .class files and want Eclipse to use this directory for a specific project as another entry to CLASSPATH:
Project->Properties->Java Build Path, Tab Libraries->Add Class Folder

Plugins

Documentation

Java && Crypto

How to create Java-based web-applications

I wrote a mini-HOWTO on setting up and creating a Java-based web-application, using Tomcat, Turbine, Torque, Velocity and MySQL.

Hints on the Language

$CLASSPATH

If the environment variable CLASSPATH is not set, more recent "Java's" are smart enough to include the Java-default class-files and the current directory. If you set the CLASSPATH variable to some value, the Java-defaults will still be available, but mind to include your current directory too (if you want to) by adding the directory "." to the path. Otherwise you may be able to compile your programme ("javac Test.java"), but trying to start it ("java Test") will raise the following error (because the class file Test.class is in the current directory which is not in the CLASSPATH):

  Exception in thread "main" java.lang.NoClassDefFoundError: Test

You can also temporarily set the classpath using javac's or java's -classpath option.

String basics

string1 and string2 are the same, aren't they?

If you compare two objects by simply calling "object1 == object2", you do not test if the two objects hold the same content, but you only compare if the two references "object1" and "object2" refer to the same object!
Similarly, "object1 = object2;" does not fill "object1" with the contents of "object2", but instead you'll end up with both identifiers referencing to the "object2"-object and none to the "object1"-object (which is lost and will be garbage-collected at some point).

Having said all this, here's the string stuff:

String a = "Java";
String b = "Java";
  ==>   (a==b)     returns  'true'.
      a.equals(b)  returns  'true'.

String a = new String("Java");
String b = new String("Java");
  ==>   (a==b)     returns  'false'.
      a.equals(b)  returns  'true'.

There is a table for all string-constants, which holds each string just once.
C will do the same:
  char *c = "C-code";
  char *d = "C-code";
  ==>   (c==d)     returns  'true'.

Every Java string can be looked up in the table:
  String a = "Java";
  String b = "Ja" + "va";
  ==>   (a==b)     returns  'true'.
And:
  b = b.intern();
  ==>   (a==b)     returns  'true'.

Static Initializers

Ever wondered what hides behind Java's "Static Initializers"? Here's a good article on what you can do with them.
Thanks to Gerhard for this link.

Threads

Threads are a gift ... once you understand them. ;)
Here are possibilities of how to create threads in Java:

/* Extending class Thread */
public class MyThread extends Thread {
  public void run() {
    System.out.println("I'm a thread.");
  }
  public static void main(String[] args) {
    new MyThread().start();
  }
}


/* Implementing Runnable, version 1 */
//public class ImpRunnable1 implements Runnable {
public class ImpRunnable1 extends SomeClass implements Runnable {
  public void run() {
    System.out.println("I'm a thread.");
  }
  public static void main(String[] args) {
    new Thread(new ImpRunnable1()).start();
  }
}


/* Implementing Runnable, version 2 */
//public class ImpRunnable2 implements Runnable {
public class ImpRunnable2 extends SomeClass implements Runnable {
  private Thread thread;
  public void run() {
    System.out.println("I'm a thread.");
  }
  public void start()
  {
    /* Make this object a Thread: */
    thread = new Thread(this);
    // thread.setPriority(Thread.MAX_PRIORITY);
    thread.start();
  }
  public static void main(String[] args) {
    // Create a new object and run its start() method:
    ImpRunnable2 r = new ImpRunnable2();
    r.start();
  }
}

As you can see, implementing the Runnable interface enables our class to inherit from SomeClass and become a thread.

Useful Packages

Log4j

A great tool to have your programmes create (easily customizable) logging output is Log4j from the Apache Jakarta Project.
Many thanks to Rick for this hint!

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;

public class MyLog4j {
	private static final Logger log =
		(Logger) Logger.getInstance(MyLog4j.class.getName());

	public static void main(String[] args) {
		BasicConfigurator.configure();

		// Using DOMConfigurator, it's possible to read a log4j-config-file from a URL!
		// This way, changing this (possibly remote) config-file, you can change the
		// programme's logging behaviour, e.g. from "normal" to
		// "with debug information".

		log.info("Possible levels are: debug, info, warn, error, fatal.");

		// In case of "extreme performance needed" and lots of debug messages
		// it's recommended to use if-clauses to prevent unnecessary
		// string-concetanations etc.:
		if (log.isDebugEnabled())
			log.debug("Output debug messages only if they are enabled.");
	}
}

Ant

Ant is the "defacto standard" build tool in the Java world -- something like "make" for C.

build.xml is the Ant configuration file (similar to make's "Makefile").
The basic structure is explained and an example buildfile is shown in the Apache Ant User Manual, section "Using Ant".

Maven

Maven is another tool, slightly similar to Ant. Here's an article on Maven 2.0.

Realtime Specifications for Java (RTSJ)

Despite your most probable first thought, "Real-time and Java !? -- This doesn't mix!": There are real-time specs for Java, and one approach (the RTSJ) is officially supported by Sun.
I wrote more on RTSJ here.
(By the way, RTSJ is a vital part of my diploma thesis.)

Some other Real-Time approach to Java is Javolution.

Miscellaneous

back to index

Valid XHTML 1.0 Strict Valid CSS! Created with Vim [Blue Ribbon Campaign icon]
© Markus Amersdorfer
last modified: 2010-02-23 15:52:03
12586 hits