Java Q&A 
by Tim Kientzle

Listing One
class Foo {
   private static String outString = "My output";
   private byte[] outBytes = outString.getBytes();

   void writeOutput(ServletOutputStream socket) {
      socket.write(outBytes); // Fast!
      // socket.print(outString); // Slow!
   }
}


Listing Two
(a)
String foo() {
   StringBuffer b
      = new StringBuffer();
   b.append(...);
   b.append(...);
   return b.toString();
}

(b)
void foo(StringBuffer b) {
   b.append(...);
   b.append(...);
}


Listing Three
(a)
class A {
  Foo var;
  synchronized void update(int arg) {
     var.setProperty(arg);
  }
  synchronized void read() {
     int i = var.getProperty();
  }
}

(b)
class A {
  Foo var;
  void update(int arg) {
    Foo newVar = new Foo();
    newVar.setProperty(arg);
    var = newVar;
  }
  void read() {
    Foo temp = var;
    int i = temp.getProperty();
  }
}


Listing Four
long expiration = ...; // When this response expires
long lastModified = ...; // When this page last changed
long now = System.currentTimeMillis();
response.setDateHeader("Date",now);
response.setHeader("Cache-Control","maxage="+(expiration-now)/1000);
response.setDateHeader("Expires",expiration);
response.setDateHeader("Last-Modified",lastModified);


