Getting a list of environment variables

You can’t anymore. There used to be a method called getenv(String) in java.lang.System but it’s been deprecated since
1.1.8. The only thing you can do now is to use the -D properties flag on the command line when you start your program.

java -Dproperty=value myapp.class

Where property is the name of the property and value is the value you want to as sign it.

You can then retrieve the value of the property using:

getProperty("property");

If you still want to allow the user to set environment variables you can always use them as the value for the property in the startup batch file or shell script like this:

Windows:

   SET ENVVVAR=value
   java -Dproperty=%ENV_VAR% myapp.class

Unix:

   ENV_VAR=value
   export ENV_VAR
   java -Dproperty=$ENV_VAR myapp.class

What does serialver do?

The JDK utility serialver displays the SUID for a class. The SUID is useful if you want to allow/disallow an older version of a serialized class to be deserialized to a newer version.
When serialization occurs, the class is first examined if it has a field static final long serialVersionUID. It it exists, it will write this value to the stream. If it doesn’t exist, it will generate a 64-bit hash (using the SHA algorithm on the classname, interface, methods and fields) and use that as the serialVersionUID to write to the stream.

Main.java:

import java.io.*;

public class Main implements Serializable {
   public static void main(String args[]) {
   }
}

after compiling this class: (note that a serialVersionUID has been included, because it was not specified)

C:>serialver Main
Main:    static final long serialVersionUID = 6106269076155338045L;

You can specify the serialVersionUID for versioning of serialized objects.

Main.java:

import java.io.*;

public class Main implements Serializable {
   static final long serialVersionUID = 1;
 
   public static void main(String args[]) {
   }
}

after compiling this class:

C:\>serialver Main
Main:    static final long serialVersionUID = 1L;

Setting the initial and maximum heap size

You can use the Java command line parameters -mxX and -msX:
from Sun’s site:

-mxx
          Sets the maximum size of the memory allocation pool (the garbage collected heap) to x. The default is 16 megabytes of memory. x must be
          greater than or equal to 1000 bytes. The maximum memory size must be greater than or equal to the startup memory size (specified with the -ms
          option, default 16 megabytes). 
 
          By default, x is measured in bytes. You can specify x in either kilobytes or megabytes by appending the letter "k" for kilobytes or the letter "m"
          for megabytes. 
 
-msx 
          Sets the startup size of the memory allocation pool (the garbage collected heap) to x. The default is 1 megabyte of memory. x must be > 1000
          bytes. The startup memory size must be less than or equal to the maximum memory size (specified with the -mx option, default 16 megabytes). 
 
          By default, x is measured in bytes. You can specify x in either kilobytes or megabytes by appending the letter "k" for kilobytes or the letter "m"
          for megabytes. 

Eg.

java -ms32000k -mx32000k TestProgram

Using the Observable/Observer pattern

An Observable object is an object that contains data in which one or more Observer objects are interested in.
The Observers want to know when the state of the Observable object changes.

The implementation is simple. In the package java.util, you find the class Observable. The object that you want to be able to monitor should extend from it. For example, suppose we have a Mailbox object (that can only contain one email at a time) and we know that other objects may be interested in state changes:

import java.util.*;
 
class Mailbox extends Observable
{
   private String message;
 
   public void newMail(String message) {
      this.message = message;
      setChanged();
      notifyObservers();
   }   
 
   public String getMessage() {
      return message;
   }
}

Observable is a class that encapsulates the common details about the pattern, like being able to “register” an object that is interested. Whenever the method newMail is invoked, the state changes and the object is marked as being changed. A call to notifyObservers means to notify the Observer objects that have previously registered themselves through addObserver, a method inherited from Observable.

An Observer is an interface that all your observers need to implement to ensure the availability and accessibility of the method update(Observable obs, Object o).

In our example, suppose we have two observers to our Mailbox: a MailGui and MailLog.

class MailGui implements Observer
{
   public void update(Observable obs, Object o) {
      Mailbox mb = (Mailbox) obs;
      System.out.println("MailGui has received notification of new " +
                         "message: " + mb.getMessage());
   }
}
 
class MailLog implements Observer
{
   public void update(Observable obs, Object o) {
      Mailbox mb = (Mailbox) obs;
      System.out.println("MailLog has received notification of new " +
                         "message: " + mb.getMessage());
   }
}

The notifyObservers method in Observable will loop through a list of objects of type Observer and calls the method update sequentially on each one of them. It provides itself as the first parameter (so the Observer knows which Observable we’re talking about – it may have registered itself with different ones) and an optional object as a second parameter. This optional object comes from when you call the notifyObservers(Object o) variant.

Here’s the Main class:

import java.util.*;
 
public class Main {   
   public static void main(String[] args) {
      Mailbox mb = new Mailbox();
 
      MailGui gui = new MailGui();
      MailLog log = new MailLog();
 
      mb.addObserver(gui);
      mb.addObserver(log);
 
      mb.newMail("... email#1 ...");
      mb.newMail("... email#2 ...");
   }
}

and the output:

MailLog has received notification of new message: ... email#1 ...
MailGui has received notification of new message: ... email#1 ...
MailLog has received notification of new message: ... email#2 ...
MailGui has received notification of new message: ... email#2 ...

Decoupling

It is important to realize that when an Observable wants to notify Observers of a change through calling update on them, it is tighly coupled to it. If update decides to go do some calculations, surf the net or play SkateOrDie, all actions that take forever to complete, the Observable cannot go on notifying other interested Observers cause it happens all in the same thread.

To perform some decoupling, you should place some threaded FIFO datastructure in between the Observable and Observer. The FIFO object would then both be an Observable and Observer.
Before:

                 (update)
  [ Observer1 ] <--------- [            ]
  [ Observer2 ] <--------- [ Observable ]
  [ Observer3 ] <--------- [            ] 

After:

                 (update)
  [ Observer1 ] <--------- [                 ]  (update)
  [ Observer2 ] <--------- [ FIFO Observable ] <--------- [ Observable ]
  [ Observer3 ] <--------- [                 ]   

Creating a thread-safe singleton class

The best way is:

public class ExampleSingleton {
 
  private static ExampleSingleton instance;
 
  public static ExampleSingleton getInstance() {
    if( instance == null ) {
      synchronized( ExampleSingleton.class ) {
        if ( instance == null ) {
           instance = new ExampleSingleton();
        }
      }//sync ends
    }
 
    return instance;
  }
}

to make it threadsafe you have to use synchronization. But if you sync the whole method, you will create a tiny unnecessary performance overhead. Watch that the second if-condition is vitally important. Lets say instance is null, at this point two threads are executing the method. one thread goes in the sync block and the other starts waiting. when the second thread gets the lock and enters sync block, the Singleton has already been creates by the first thread. If you ommit the second if-condition, the second thread will create a second instance of the singleton.

Hope it helps.

Quicksort in Java

There are numerous sites available that explain the details and lots of working sources available on the net.

http://developer.novell.com/ndk/doc/samplecode/njcl_sample/NSIBrowser/QuickSort.java.html
http://www.ececs.uc.edu/~franco/C321/html/quicksort.java.html
http://www.math.grin.edu/~rebelsky/Courses/CS152/98S/Labs/lab.09.html
http://www.cs.colorado.edu/~main/javawork/Quicksort.html
http://www.cs.may.ie/~pmooney/QUICKSORT.htm