Friday, December 16, 2005

Java Unicode Support

Check the JSR-204 for java unicode support : JSR-204

Supplementary Character Support Approach
  • Use the primitive type int to represent code points in low-level APIs, such as the static methods of the Character class.

  • Interpret char sequences in all forms (char[], implementations of java.lang.CharSequence, implementations of java.text.CharacterIterator) as UTF-16 sequences, and promote their use in higher-level APIs.

  • Provide APIs to easily convert between various char and code point based representations.


Good blog on unicode support in j2se5 : John Conner blog
Highlights:
# char is a UTF-16 code unit, not a code point
# new low-level APIs use an int to represent a Unicode code point
# high level APIs have been updated to understand surrogate pairs
# a preference towards char sequence APIs instead of char based methods

Java Floating point

Good link for java floating point : http://people.uncw.edu/tompkinsj/133/Numbers/Reals.htm

IEEE 754:
The IEEE 754 Standard uses 1-plus form of the binary normalized fraction (rounded). The fraction part is called the mantissa.
1-plus normalized scientific notation base two is then

N = ± (1.b1b2b3b4 ...)2 x 2+E

The 1 is understood to be there and is not recorded.
The Java primitive data type float is 4 bytes, or 32 bits:

Sign: 0 ® positive, 1 ® negative.

Exponent: excess-127 format for float, excess-1023 format for double.

* Float: Emin = -126, Emax = 127
* Double: Emin = -1022, Emax = 1023
* Consider an 8 bit number which has a range of 0 - 256. We use the formula excess - 127 = E to assign the value of our exponent with excess = 127 representing 0.
* In this manner, excess = 120 is an exponent of -7 since 120 - 127 = -7, and excess = 155 is the exponent +28 since 155 - 127 = +28. Excess values of 0, (Emin - 1), and 255, (Emax + 1), have special meanings which are discussed below.

Mantissa: normalized 1-plus fraction with the 1 to the left of the radix point not recorded, float: b1b2b3b4…b23, double: b1b2b3b4…b52. This value is rounded based on the value of the next least significant bit not recorded (if there is a 1 in b24, b53 respectively, increment the least significant bit).

Thursday, December 15, 2005

Static Method Inheritance

This link : http://java.sun.com/docs/books/tutorial/java/javaOO/override.html explains this issue with details.

For class methods, the runtime system invokes the method defined in the compile-time type of the reference on which the method is called. For instance methods, the runtime system invokes the method defined in the runtime type of the reference on which the method is called.

I paste here the same for easy reference:

public class Animal {
public static void hide() {
System.out.println("The hide method in Animal.");
}
public void override() {
System.out.println("The override method in Animal.");
}
}

The second class, a subclass of Animal, is called Cat:

public class Cat extends Animal {
public static void hide() {
System.out.println("The hide method in Cat.");
}
public void override() {
System.out.println("The override method in Cat.");
}

public static void main(String[] args) {
Cat myCat = new Cat();
Animal myAnimal = (Animal)myCat;
myAnimal.hide();
myAnimal.override();
}
}

The Cat class overrides the instance method in Animal called override and hides the class method in Animal called hide. The main method in this class creates an instance of Cat, casts it to a Animal reference, and then calls both the hide and the override methods on the instance. The output from this program is as follows:

The hide method in Animal.
The override method in Cat.

The version of the hidden method that gets invoked is the one in the superclass, and the version of the overridden method that gets invoked is the one in the subclass. For class methods, the runtime system invokes the method defined in the compile-time type of the reference on which the method is called. In the example, the compile-time type of myAnimal is Animal. Thus, the runtime system invokes the hide method defined in Animal. For instance methods, the runtime system invokes the method defined in the runtime type of the reference on which the method is called. In the example, the runtime type of myAnimal is Cat. Thus, the runtime system invokes the override method defined in Cat.

An instance method cannot override a static method, and a static method cannot hide an instance method.