Friday, 5 December 2008

IntelliJ Keyboard Shortcuts

IntelliJ is a very fast editor from jetbrains and does remote debugging more than twice as fast as Eclipse. Searching is also slicker than Eclipse. Here are some handy shortcuts for jet-setting across your source code.

Cntrl-N Jump to class
Cntrl-E Scroll through open files
Cntrl-F and F3 Search and repeat last search
Cntrl-F12 Jump through class (fields and methods)

Alt-F7 Find usages (also click "Open in new tab" to get tabbed search)
Alt-left, Alt-right Scroll through panes (Cntrl-E if you want to jump >1 step at a time)
Cntrl-B Jump to DECLARATION
Cntrl-Alt-B Jump to IMPLEMENTATION

Alt-3 go to Find screen

The Cntrl-MouseUp/MouseDown combination can be used to dynamically zoom in/out of the current pane. This is the same key combination in Internet Explorer for the same operation.

The founder of JetBrains is Sergey Dmitriev whose hobbies include pure mathematics, including functional analysis. here's an interview with him. Official bio is here.

Thursday, 4 December 2008

Basics of java.lang

rt.jar contains a lot of the core Java classes, such as those in java.lang, and java.net. When learning the core classes think of what each one is and what they are used for. Try and think of at least three examples in each case (use Google code search to help you).

Examples:

java.lang.Character (implements Serializable, Comparable). Character is a wrapper for a char. Useful e.g. when processing a character input from stdin or from gnu.getopt. Characters can be converted to strings using toString().

java.lang.System (extends Object). This fundamental class contains sytem-wide operations that act on the currently running virtual machine.

  • Exit the VM. exit(statusCode) terminates the VM. A value other than zero indicates abnormal termination
  • Bring in code from a shared library. static void load(String filename), static void loadLibrary(String libname)
  • Garbage collect. System.gc() prompts the VM to run the garbage collector.
  • Reference std input, stderr and stdout. This is done via the static PrintStream fields System.in, System.err and System.out
  • Restrict sensitive operations. This can be done by creating a SecurityManager and calling setSecurityManager on the System class.

java.lang.System is the moral equivalent of java.lang.Runtime; both classes facilitate interaction with the currently running VM. Many methods on System e.g. System.gc, can also be called on the Runtime class, e.g. Runtime.gc.

Coding Conventions - Lower Camel Case and Upper Camel Case

Java generally uses "lower camel case" (LCC) for method names. Example: br.readLine();
C# generally uses "upper camel case" (UCC) for method names. Example: sr.ReadlLine();

This is also reflected in the use of "main" in Java and "Main" in C#.

Strings are represented by "class String" in both Java and C#. The difference is in C# String is aliased to string which is more natural to use and similar to C++.

One of the earliest commercial examples of camel case is the 1950s CinemaScope film projection system.

Old School java.io

Java's BufferedReader extends the abstract Reader class, which reads character streams.

import java.io.BufferedReader;
import java.io.FileReader;

try {
BufferedReader br = new BufferedReader( new FileReader( someFile ) );
while ( br.ready() ) {
String line = br.readLine();
// processLine(line)
}

br.close();
} catch (Exception e) {}

read() will read one character, readline() will read a line. Don't forget to close the reader when you're done. The BufferedReader has two constructors, one that takes a "regular" Reader object, and the other that takes a Reader and the size of the buffer.

Tuesday, 2 December 2008

To sun.misc.Signal or not sun.misc.Signal?

In Java an application can install its own signal handler via sun.misc.Signal. This is deprecated in Java 1.6.

http://www.ibm.com/developerworks/java/library/i-signalhandling/

Sunday, 30 November 2008

Java New IO: ByteBuffers

java.nio.ByteBuffer. Provides a range of operations:

get, put - read and write single byte, wrap - wraps a byte array into a buffer.



ByteBuffer is derived from the abstract class Buffer (aka java.nio.Buffer). "A buffer is a linear, finite sequence of elements of a specific primitive type". Properties include: capacity (number of items it contains, never negative and never changes), limit (index of first element that should not be read or written), position (index of next item to be read/written).

A byte buffer can be direct or non direct. Given a direct byte buffer, the JVM will do its best to do native I/O operations on it. This will avoid copying buffer's content to an intermediate buffer before invoking the O/S underlying I/O operations. There is a factory method allocateDirect(int capacity) that allocates a new direct byte buffer. If the capacity is negative we throw IllegalArgumentException.

Messaging Concepts (JMS etc)

JMS.

JMS is a layer on top an underlying messaging engine. JBoss, for example, has a messaging engine that implements the JMS specification, allowing transparent access to the messaging system.

Tibco.

Tibrv class has static open and close methods to create and destroy a Tibco "environment". getVersion returns the current version of Tibco for Java as a String. defaultQueue accesses the default queue (every process has exactly one default queue which you must not destroy).

Your RV client application can connect to a network service via a TibrvRvdTransport object that connects to an rvd (RV daemon process) or a TibrvRvaTransport which connects to an rva (RV agent process) that in turn connects to an RVD. The critical question -How does an RVD work? Basically, a program sends a message and RVD transmits it across the network. It sends messages with matching subject names to the appropriate listeners. (RVAs are used as intermediaries between RVDs and applications - precluding the need for Tibco (JNI) shared libraries to be installed on the client machine). They are used as a communication mechanism for Java applets to talk to Tibco machinery. RVAs add an extra level of indirection and are thus not as efficient as talking directly to a RVD. The RVD is basically a software router.

(note: If a transport can't connect to an rvd transport process it will automatically start one.)