Saturday 9 August 2008

The Clob Interface

Don't put SQL (Advanced) + JDBC (Expert) on your resume if you don't know what a Clob is.

What does the Class Class implement and extend?

This refers to java.lang.Class. Here's the declaration:

public final class Class
extends Object
implements Serializable

To print out the class name of an object you can do:

obj.getClass().getName()

Another common sight is calling the static method forName which returns the Class object (a.k.a. runtime class descriptor) associated with the given string name. By default, forName uses the class loader of the current class. If the class cannot be found, a ClassNotFoundException is thrown. forName can also throw a LinkageError and ExceptionInInitializer error.

In order to return the class object (or class descriptor) the class needs to loaded, linked and initialized. Thus Class.forName is sometimes used just to load and link a class then throw away the returned object.

The Class Class also contains boolean methods like isInterface and isPrimitive.

Friday 8 August 2008

What is Dependency Injection?

Dependency injection (DI) is a specific form of Inversion of Control (IoC).

Programs utilising IoC are much harder to reason about than straightforward sequential programs.

Thursday 7 August 2008

Hacking Java DB

Introduction to Java DB

Java DB is a pretty mature technology, that evolved out of IBM Cloudscape.
I like it better than some of the other offerings out there like HypersonicDB.

Java DB 10.4 using JDBC4 has a documented API and developers guide.
It uses an Apache 2.0 license. If you need to recap what JDBC 4.0 is all about, here's an old article on Artima.com with explanations.

To verify Derby has been properly installed on your system use the sysinfo tool.

java org.apache.derby.tools.sysinfo

should return your Java version, vendor and other configuration information.

Getting started with Java DB using ij

ij is the interactive Java tool for querying Derby. To test it out create and connect to a new database as follows.

ij> connect 'jdbc:derby:MyDbTest;create=true';
ij> exit;

This creates a directory MyDbTest and a file derby.log. Now you can connect to the new database:

ij> connect 'jdbc:derby:MyDbTest';
ij> show schemas;

will show all the schemas in the database e.g. APP, SYS, SQLJ.

Aliasing Makes Life Easier

Tip: to make the shell automatically recognise the Derby protocol, you can alias ij to the following:

alias ij="java -Dij.protocol=jdbc:derby: org.apache.derby.tools.ij"

See how easy this makes creating and connecting to a database:

connect 'TestDb;create=true';
connect 'TestDb';

Brilliant!

Note: -D is the standard way to set command-line flags in ij. If you have more than one flag to set, just do -D multiple times.

Embedded and Client-server Mode

Derby has both an embedded and client-server mode. In embedded mode, a Derby database may not be used concurrently between two VMs.

Semantics of java -jar

When we run jar files we can do so using java -jar. But what are the implications of this on classpath etc.?

The JAR (Java ARchive) file will contain a Manifest stating where the main method is located. JAR files provide compression for efficient storage.

What does JNI work? If I have a C++ library how do I access that from Java?

Sheng Liang has written a book on JNI divided into three parts:
The Programmers Guide should be read carefully.

Know - what is the JNIEnv interface pointer, what is the CPointer class and CMalloc class.

What is JPA?

JPA = Java Persistence API, standardises persistence and ORM in Java (integrates approaches from the Hibernate ORM library, JDO and Oracle's TopLink).

JPA is implemented in the GlassFish application server, from code contributed by Oracle.

Wednesday 6 August 2008

How does ant build Java code?

JDK code samples can be built either with NetBeans or ant (ant.apache.org) using the build.xml file. A similar tool called nant is used for .NET.

How does Eclipse build Java code?

Eclipse has its own Java compiler (the Eclipse Compiler for Java) that implements the Java Language Specification.

Compliance level can be checked using Alt-WP and clicking Java->Compiler.

Compiler->Errors and Warnings allows you to fine-tune what the compiler regards as error versus a warning. e.g. in my setup: non-static access to a static member is a warning, undocumented empty block is ignored.

The compiler is part of the JDT (Java development tools) extension to the workbench. It is not possible to use another Java compiler other than the built-in one (which includes support for special features like automatic incremental compilation and code snippet evaluation).

A programmers guide to JDT tooling can be found here.

What is the invokespecial JVM instruction?

invokespecial and invokevirtual are two different beasts - both call instance methods.

invokevirtual will invoke an instance method, with dispatch based on the class of the object reference. If a method can't be found an AbstractMethodError is raised.

invokespecial contains special handling for:
  1. superclass method invocations (actually checks the resolved method's class is a superclass of the current class)
  2. private method invocation
  3. instance initialization invocations ( methods, which are void functions generated from constructors)

Monday 4 August 2008

Who are the top Java / SUN bloggers?

Rich Green, head (& Executive VP) of software at SUN:
http://blogs.sun.com/richgreen/
Rich Green is a long-standing guy at SUN, having been a general manager of Solaris products. He was at SUN for 14 years before moving to Bill Coleman's Cassatt Corporation, specialising in virtualisation technologies. (At Cassatt, Rich was EVP of Product Development). Before leaving SUN he was a key witness in the antitrust case with Microsoft, resulting in a $1.6bn settlement and 10-year collaboration agreement.

James Gosling's ("Father of Java - the language formerly known as Oak") blog :
http://blogs.sun.com/jag
James got his PhD from CMU in 1983. He did the original design for the Java programming language and wrote its first compiler and VM. He is also the author of emacs.

Jonathan Schwartz, SUN's CEO, talks about various things including acquisition of MySQL AB:
http://blogs.sun.com/jonathan/entry/winds_of_change_are_blowing
Jonathan has a consulting background from McKinsey and replaced the previous CEO Scott McNealy who was also a co-founder in 1982, along with Vinod Khosla (now running "venture assistance" firm, Khosla Ventures), Bill Joy (author of Berkeley UNIX) and Andreas ("Andy") von Bechtolsheim (UNIX workstation innovator). Vauaghan Pratt, a Stanford University professor, advised the company.

Mike Dillon, SUN's General Counsel:
http://blogs.sun.com/dillon/
JD from University of Santa Clara, has worked for a number of law firms in the Bay Area.

Michelle Dennedy, SUN's CPO:
http://blogs.sun.com/suncpo/
Has both a legal and science background.

Sunday 3 August 2008

What's an iterator and what are the rules of thumb?

Some collection classes can be traversed via a java.util.Iterator interface. It is not generally advisable to modify a collection when traversing it via an iterator.

Blog Archive