Saturday, October 8, 2011

Core Java

Java Hashtable

Hashtable is an implementation of a key-value pair data structure in java. You can store and retrieve a ‘value’ using a ‘key’ and it is an identifier of the value stored. It is obvious that the ‘key’ should be unique.
java.util.Hashtable extends Dictionary and implements Map. Objects with non-null value can be used as a key or value. Key of the Hashtable must implement hashcode() and equals() methods. By the end of this article you will find out the reason behind this condition.Hashtable
Generally a Hashtable in java is created using the empty constructor Hashtable(). Which is a poor decision and an often repeated mistake. Hashtable has two other constructors
Hashtable(int initialCapacity)
and
Hashtable(int initialCapacity, float loadFactor)
. Initial capacity is number of buckets created at the time of Hashtable instantiation. Bucket is a logical space of storage for Hashtable.

Hashing and Hashtable

Before seeing java’s Hashtable in detail you should understand hashing in general. Assume that, v is a value to be stored and k is the key used for storage / retrieval, then h is a hash function where v is stored at h(k) of table. To retrieve a value compute h(k) so that you can directly get the position of v. So in a key-value pair table, you need not sequentially scan through the keys to identify a value.
h(k) is the hashing function and it is used to find the location to store the corresponding value v. h(k) cannot compute to a indefinite space. Storage allocated for a Hashtable is limited within a program. So, the hasing function h(k) should return a number within that allocated spectrum (logical address space).

Hashing in Java

Java’s hashing uses uses hashCode() method from the key and value objects to compute. Following is the core code from Hashtable where the hashCode ‘h’ is computed. You can see that both key’s and value’s hashCode() method is called.
h += e.key.hashCode() ^ e.value.hashCode();
It is better to have your hashCode() method in your custom objects. String has its own hashCode methode and it computes the hashcode value as below:
s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
If you don’t have a hashCode() method, then it is derived from Object class. Following is javadoc comment of hashCode() method from Object class:
* Returns a hash code value for the object. This method is
   * supported for the benefit of hashtables such as those provided by
   * <code>java.util.Hashtable</code>.
If you are going to write a custom hashCode(), then follow the following contract:
* The general contract of <code>hashCode</code> is:
    * <ul>
    * <li>Whenever it is invoked on the same object more than once during
    *     an execution of a Java application, the <tt>hashCode</tt> method
    *     must consistently return the same integer, provided no information
    *     used in <tt>equals</tt> comparisons on the object is modified.
The following is to improve performance of the Hashtable.
* <li>If two objects are equal according to the <tt>equals(Object)</tt>
   *     method, then calling the <code>hashCode</code> method on each of
   *     the two objects must produce the same integer result.
hashCode() guarantees distinct integers by using the internal address of the object.

Collision in Hashtable

When we try to restrict the hashing function’s output within the allocated address spectrue limit, there is a possibility of a collision. For two different keys k1 and k2, if we have h(k1) = h(k2), then this is called collision in hashtable. What does this mean, our hashing function directs us store two different values (keys are also different) in the same location.
When we have a collision, there are multiple methodologies available to resolve it. To name a few hashtable collision resolution technique, ‘separate chaining’, ‘open addressing’, ‘robin hood hashing’, ‘cuckoo hashing’, etc. Java’s hashtable uses ‘separate chaining’ for collision resolution in Hashtable.

Collision Resolution in java’s Hashtable

Java uses separate chaining for collision resolution. Recall a point that Hashtable stores elements in buckets. In separate chaining, every bucket will store a reference to a linked list. Now assume that you have stored an element in bucket 1. That means, in bucket 1 you will have a reference to a linked list and in that linked list you will have two cells. In those two cells you will have key and its corresponding value.Hashtable Collision
Why do you want to store the key? Because when there is a collision i.e., when two keys results in same hashcode and directs to the same bucket (assume bucket 1) you want to store the second element also in the same bucket. You add this second element to the already created linked list as the adjacent element.
Now when you retrieve a value it will compute the hash code and direct you to a bucket which has two elements. You scan those two elements alone sequentially and compare the keys using their equals() method. When the key mathches you get the respective value. Hope you have got the reason behind the condition that your object must have hashCode() and equals() method.
Java has a private static class Entry inside Hashtable. It is an implementation of a list and you can see there, it stores both the key and value.

Hashtable performance

To get better performance from your java Hashtable, you need to
1) use the initialCapacity and loadFactor arguments
2) use them wisely
while instantiating a Hashtable.
initialCapacitiy is the number of buckets to be created at the time of Hashtable instantiation. The number of buckets and probability of collision is inversly proportional. If you have more number of buckets than needed then you have lesser possibility for a collision.
For example, if you are going to store 10 elements and if you are going to have initialCapacity as 100 then you will have 100 buckets. You are going to calculate hashCoe() only 10 times with a spectrum of 100 buckets. The possibility of a collision is very very less.
But if you are going to supply initialCapacity for the Hashtable as 10, then the possibility of collision is very large. loadFactor decides when to automatically increase the size of the Hashtable. The default size of initialCapacity is 11 and loadFactor is .75 That if the Hashtable is 3/4 th full then the size of the Hashtable is increased.
New capacity in java Hashtable is calculated as follows:
int newCapacity = oldCapacity * 2 + 1;
If you give a lesser capacity and loadfactor and often it does the rehash() which will cause you performance issues. Therefore for efficient performance for Hashtable in java, give initialCapacity as 25% extra than you need and loadFactor as 0.75 when you instantiate.

Java Closures

In this article, I will explain what a closure is and clear the confusion surrounding anonymous inner classes and closures, then java’s current situation with respect to closures.
First I want to emphasize the below two points:
  1. As of JDK 1.6 we don’t have closures in java.
  2. Annonymous inner classes in java are not closures.

Definition of closure

Function types and inline function-valued expression are called closures. Let me decrypt this definition for you. An anonymous function that contains some context surrounding it as a snapshot and can be passed as a parameter. This closure defintion has two parts. First one is about callback. That is, a pointer to a function that can be passed as a parameter. Second part of the definition is, this callback function will enclose some contextual information surrounding it as a snapshot and passed along with the function.
A closure is also referred to as a ‘first class object’ which can refer to attributes from its enclosing scope. As defined by Christopher Strachey in ‘Understanding Programming Languages’, a first class object can be stored in a data structure, passed as a parameter, can be returned from a function, can be constructed at runtime and independent of any identity.

In case if you are curious in Mathematics a closure is, when you operate on a members fo a set and if the resultant is always a member of that set then its called a closure.

Anonymous inner class is not closure

Anonymous classes in java are close to being called as a closure. They don’t 100% support the definition but come close to it and thats why we see lot of literature calling anonymous inner classes as closure. Why do I say its not 100%? An anonymous inner class can access “only” the final local variable of the enclosing method. It is because of this restriction, anonymous inner class in java is not a closure.
If you remember the memory management in java, you can recall that the local variables are stored in a stack. These java stacks are created when the method starts and destroyed when it returns. Unlike local variables, final fields are stored in method area which lives longer even after the return of the method. If we want to make anonymous inner class as a closure, then we should allow it to access all the fields surrounding it. But, as per the current memory management, they will be destroyed and will not be accessible after the method has returned.

Closure in Java

In that case will we get closure in java in future? We have a specification written by Peter Ahe, James Gosling, Neal Gafter and Gilad Bracha on closures for java. It gives detailed description of how a closure can be implemented in java and example code on how to use them. We have JSR 335 for closures in java named as Lambda Expressions for the Java Programming Language.

Java Array

When you need to store same ‘type’ of data that can be logically grouped together, then you can go for java array.
For example, imagine if you had to store the list of countries in individual java variables and manipulate them. You will have more than hundred variables to manage. Can’t imagine, it would be tedious in any language. Have you ever seen a decent size application programmed without arrays?
In this article you will not find nothing new about java array. Just the same old theory to refresh and I will try to be comprehensive. If you expect some surprise or hidden thing from java, please ALT-F4.
java array logical view
Array is one among the many beautiful things you have in a programming language. Easy to iterate, easy to store and retrieve using their integer index. In java, nobody can deny using an array I challenge. I know you all started with public static void main(?). The argument for the main method is an array.
In java arrays are objects. All methods of an Object can be invoked on an array. Arrays are stored in heap memory.
java array - physical view
Though you view the array as cells of values, internally they are cells of variables. A java array is a group of variables referenced by a common name. Those variables are just references to a address and will not have a name. These are called the ‘components’ of a java array. All the components must be of same type and that is called as ‘component type’ of that java array.

Java Array Declaration

int []marks;
or
int marks[];

Java Array Instantiation

marks = new int[5];
5 inside the square bracket says that you are going to store five values and is the size of the array ‘n’. When you refer the array values, the index starts from 0 ‘zero’ to ‘n-1′. An array index is always a whole number and it can be a int, short, byte, or char.
Once an array is instantiated, it size cannot be changed. Its size can be accessed by using the length field like .length Its a java final instance field.
All java arrays implements Cloneable and Serializable.

Java array initialization and instantiation together

int marks[] = {98, 95, 91, 93, 97};

ArrayIndexOutOfBoundsException

One popular exception for java beginners is ArrayIndexOutOfBoundsException. You get ArrayIndexOutOfBoundsException when you access an array with an illegal index, that is with a negative number or with a number greater than or equal to its size. This stands second next to NullPointerException in java for popularity.

Java Array Default Values

After you instantiate an array, default values are automatically assigned to it in the following manner.
  • byte – default value is zero
  • short – default value is zero
  • int – default value is zero
  • long – default value is zero, 0L.
  • float – default value is zero, 0.0f.
  • double – default value is zero, 0.0d.
  • char – default value is null, ‘\u0000′.
  • boolean – default value is false.
  • reference types – default value is null.
Notice in the above list, the primitives and reference types are treated differently. One popular cause of NullPointerException is accessing a null from a java array.

Iterating a Java Array

public class IterateJavaArray {
  public static void main(String args[]) {
    int marks[] = {98, 95, 91, 93, 97};
    //java array iteration using enhanced for loop
    for (int value : marks){
      System.out.println(value);
    }
  }
}
In language C, array of characters is a String but this is not the case in java arrays. But the same behaviour is implemented as a StringBuffer wherein the contents are mutable.
ArrayStoreException – When you try to store a non-compatible value in a java array you get a ArrayStoreException.

Multidimensional Arrays

When a component type itself is a array type, then it is a multidimensional array. Though you can have multiple dimension nested to n level, the final dimension should be a basic type of primitive or an Object.
int[] marks, fruits, matrix[];
In the above code, matrix is a multidimensional array. You have to be careful in this type of java array declaration.
Internally multidimensional java arrays are treated as arrays of arrays. Rows and columns in multidimensional java array are completely logical and depends on the way you interpret it.
Note: A clone of a java multidimensional array will result in a shallow copy.

Iterate a java multidimensional array

Following example source code illustrates on how to assign values and iterate a multidimensional java array.
public class IterateMultiDimensionalJavaArray {
  public static void main(String args[]) {
    int sudoku[][] = { { 2, 1, 3 }, { 1, 3, 2 }, { 3, 2, 1 } };
    for (int row = 0; row < sudoku.length; row++) {
      for (int col = 0; col < sudoku[row].length; col++) {
        int value = sudoku[row][col];
        System.out.print(value);
      }
      System.out.println();
    }
  }
}

Sort a Java array

java api Arrays contains static methods for sorting. It is a best practice to use them always to sort an array.
import java.util.Arrays;
public class ArraySort {
  public static void main(String args[]) {
    int marks[] = { 98, 95, 91, 93, 97 };
    System.out.println("Before sorting: " + Arrays.toString(marks));
    Arrays.sort(marks);
    System.out.println("After sorting: " + Arrays.toString(marks));
  }
}
//Before sorting: [98, 95, 91, 93, 97]
//After sorting: [91, 93, 95, 97, 98]

Copy a Java array

You can use the following options to copy a java array:
  • As illustrated above you can use the util calls Arrays. It contains copyOf method for different java types.
  • The most used class by a java beginner ‘System‘ has a static method to copy an array.
  • Using its clone method you can copy a java array. If the java array is multidimensional, it will be a shallow copy.
  • Write your own for loop iterating through the java array and copy elements yourself. (least preferred)

Java Double Brace Initialization

Double brace initialization is a combination of two separate process in java. There are two { braces involved in it. If you see two consecutive curly braces { in java code, it is an usage of double brace initialization.

Java Double Brace Initialization
First brace is creation of an anonymous inner class. Without considering the second brace if you see the first brace alone then its nothing new for us. We have created many anonymous inner classes in such a way.
Second brace is an initialization block. That too you have seen in it a class for initialization. You may have used a static initialization block. When you use the initialization block for an anonymous inner class it becomes java double brace initialization. The inner class created will have a reference to the enclosing outer class. That reference can be used using the ‘this’ pointer.

Example for double brace initialization

class JavaPapers {
....
....
add(new JPanel() {{
setLayout(...);
setBorder(...);
...
}}
);
}

Uses of double brace initialization

Using double brace initialization, we can initialize collections. Though people say, it is easier to initialize a constant collection using double brace initialization. I don’t see any advantage in using double brace initialization to initialize collections. In jdk 1.7 there is going to be an exclusive construct for that. Then java double brace initialization will become completely obsolete.
...
myMethod(
  new ArrayList<String>() {{
     add("java");
     add("jsp");
     add("servlets");
  }}
);
...

Note on double brace initialization

Serialization of non-static inner class has an issue. The outer class will also get serialized. Because the inner class has got a reference to the outer class and it is implicit. Since it is implicit, it cannot be marked as transient. This issue is also applicable for double brace initialization.

Java (JVM) Memory Types

Java has only two types of memory when it comes to JVM. Heap memory and Non-heap memory. All the other memory jargons you hear are logical part of either of these two.

Heap Memory

Class instances and arrays are stored in heap memory. Heap memory is also called as shared memory. As this is the place where multiple threads will share the same data.

Non-heap Memory

It comprises of ‘Method Area’ and other memory required for internal processing. So here the major player is ‘Method Area’.

Method Area

As given in the last line, method area is part of non-heap memory. It stores per-class structures, code for methods and constructors. Per-class structure means runtime constants and static fields.
The above three (heap memory, non-heap memory and method area) are the main jargon when it comes to memory and JVM. There are some other technical jargon you might have heard and I will summarize them below.

Memory Pool

Memory pools are created by JVM memory managers during runtime. Memory pool may belong to either heap or non-heap memory.

Runtime Constant Pool

A run time constant pool is a per-class or per-interface run time representation of the constant_pool table in a class file. Each runtime constant pool is allocated from the Java virtual machine’s method area.jvm memory

Java Stacks or Frames

Java stacks are created private to a thread. Every thread will have a program counter (PC) and a java stack. PC will use the java stack to store the intermediate values, dynamic linking, return values for methods and dispatch exceptions. This is used in the place of registers.

Memory Generations

HotSpot VM’s garbage collector uses generational garbage collection. It separates the JVM’s memory into and they are called young generation and old generation.

Young Generation

Young generation memory consists of two parts, Eden space and survivor space. Shortlived objects will be available in Eden space. Every object starts its life from Eden space. When GC happens, if an object is still alive and it will be moved to survivor space and other dereferenced objects will be removed.

Old Generation – Tenured and PermGen

Old generation memory has two parts, tenured generation and permanent generation (PermGen). PermGen is a popular term. We used to error like PermGen space not sufficient.
GC moves live objects from survivor space to tenured generation. The permanent generation contains meta data of the virtual machine, class and method objects.

Discussion:

Java specification doesn’t give hard and fast rules about the design of JVM with respect to memory. So it is completely left to the JVM implementers. The types of memory and which kind of variable / objects and where they will be stored is specific to the JVM implementation.

Key Takeaways

  • Local Variables are stored in Frames during runtime.
  • Static Variables are stored in Method Area.
  • Arrays are stored in heap memory.

References:

Java Primitive

As of the Java Virtual Machine Specification second edition, numeric types, the boolean type (§3.3.4), and the returnAddress type are the three java primitive types supported by JVM.
Most of you may get annoyed, we all know about the primitives of java. That is where from we all started it. But, you may not be aware of a primitive called returnAddress. Surprise isn’t it? This post serves just to bring that primitive to your notice. It is not part of the language construct / api and is not of direct use to an application programmer. But it is good to be aware of the primitive for the sake of completeness.

1) Numeric Types

Numeric types are classified as integral primitive and floating point type primitives
Integral type primitive:
byte -  8-bit signed two’s complement integers: -128 to 127 (-2power7 to 2power7 – 1)
short – 16-bit signed two’s complement integers: -32768 to 32767 (-2power15 to 2power15 – 1)
int – 32-bit signed two’s complement integers
long – 64-bit signed two’s complement integers
char – 16-bit unsigned integers representing Unicode characters (§2.1)
Value ranges from -2power(N-1) to 2power(N-1) – 1 ; where N is the bit size like 8 or 16,…
In the above, two’s complement means, a negative number will be denoted by the two’s complent of its absolute value. Most significat digit (MSB) will denote if the number is positive or negative. MSB will be 0 if the number is positive and 1 if it is negative.
Floating-point primitives are float and double
  • positive and negative sign-magnitude numbers
  • positive and negative zeros
  • positive and negative infinities
  • a special Not-a-Number value (used to represent zero/zero kind of numbers).

2) boolean type primitive

encode the truth values true and false. Even booleans are in turn processed using int instructions.

3) returnAddress type primitive

returnAddress types are pointers to the opcodes of JVM instructions like jsr, ret, and jsr_w

Type Erasure

Java generics uses Types. If you are not still using java generics, following example code explains you a simple generics usage:
package com.javapapers.sample;
import java.util.HashMap;
import java.util.Map;
public class TypeErasure {
  public static void main(String args[]) {
    Map<String, String> languageMap = new HashMap<String, String>();
    languageMap.put("1954", "FORTRAN");
    languageMap.put("1958", "LISP");
    languageMap.put("1959", "COBOL");
    String language = languageMap.get("1954");
    System.out.println("Our favourite language is "+language);
  }
}
In the above code, we instantiate a map saying that the key and values will be String. These are type parameters. It provides you type safety.
Type erasure is a process to remove the types and map it to byte code. Just trying to give a formal definition for type erasure ;-) Type erasure happens at compile time. Java compiler removes those generic type information from source and adds casts as needed and delivers the byte code. Therefore the generated byte code will not have any information about type parameters and arguments. It will look like a old java code without generics. You want to have a look at it?

New as Old!
Compile above given source and get a class file. Then de-compile that generate class file and you will know what is inside. You may use JAD to de-compile. Else you can avoid the round trip and use the JDK itself directly on java source and find out how the compiled source will look like. javac -XD-printflat -d .java
package com.javapapers.sample;
import java.io.PrintStream;
import java.util.HashMap;
import java.util.Map;
public class TypeErasure
{
    public TypeErasure()
    {
    }
    public static void main(String args[])
    {
        Map languageMap = new HashMap();
        languageMap.put("1954", "FORTRAN");
        languageMap.put("1958", "LISP");
        languageMap.put("1959", "COBOL");
        String language = (String)languageMap.get("1954");
        System.out.println((new StringBuilder("Our favourite language is ")).append(language).toString());
    }
}
You should note two things in the above de-compiled code.
  1. First thing is, “<String, String>” is missing.
  2. Second thing is (String)languageMap.get(“1954″) a type cast is added.
This is done by the java compiler while compiling java source code to byte code. This process is called java type erasure.
Main reason behind having type erasure at compile time is to give compatibility with old versions of java code where you don’t have generics. If you look at the definition of Map intereface using source available in JDK (beyond 1.5 ), it will be like
public interface Map<K,V> {
So if you use the Map without generics that should work with latest code. Thats why the java type erasure is brought in. When you use generics compiler checks whether you use all you type properly or not. If something is wrong you get an error at compile time and you need not wait until run time and blow up your production.

Consequences of Type Erasures

A java class with parametrized type cannot be instantiated as it requires a call to constructor. At run-time the type is not available because of type erasure and the instantiation cannot be done.
T instantiateElementType(List<T> arg)
{
  return new T(); //causes a compilation error
}
Because of type erasure, at run-time you will not be able to find out what was the predefined type. There are hacks available using Reflection, but it is not guaranteed that it will work in all cases. It is also not a formal way.
There is lots of discussions on whether you need type erasure or is it implemented in a good way is going around. Leave that aside. If you use java, its better to use generics as much as possible. Advantage you get in using generics is type safety and clarity in your code. Though type erasure is good or bad, better know about it. Because it is the one that translates your generic java code to byte code.

Address of a Java Object

In conventional java programming, you will never need address or location of a java object from memory. When you discuss about this in forums, the first question raised is why do you need to know the address of a java object? Its a valid question. But always, we reserve the right to experiment. Nothing is wrong in exploring uncharted areas.
I thought of experimenting using a little known class from sun package. Unsafe is a class that belongs to sun.misc package. For some of you the package might be new. You need not worry about it. If you have sun’s JDK then you have this class already.
When a class name is “Unsafe” in java, it calls for your attention immediately. Then I decided to dig deep into it and find what is unsafe about that class. Voila, it really opens up the pandora’s box. Its difficult to find the source of Unsafe. Get the source and look at the methods you will know what I am referring to.
Java’s security manager provides sufficient cover and ensures you don’t fiddle with memory that easily. As a first step, I thought of getting the memory location of a java object. Until the exploration, I too was 100% confident that it was not possible to find the location / address of an object in java.
Sun’s Unsafe.java api documentation shows us an opportunity to get the address using the method objectFieldOffset. That method says, “Report the location of a given field in the storage allocation of its class“. It also says, “it is just a cookie which is passed to the unsafe heap memory accessors“. Whatsoever, I am able to get the storage memory location of an object from the storage allocation of its class.
You can argue that, what we have got is not the absolute physical memory address of an object. But we have got the logical memory address. The following program will be quite interesting for you!
As a first step, I have to get an object of Unsafe class. It is quite difficult as the constructor is private. There is a method named getUnsafe which returns the unsafe object. Java’s security manager asks you to make your java source code privileged. I used little bit of reflection and got an instance out. I know there are better ways to get the instance. But to bypass the security easily I chose the following.
Using Unsafe’s object just invoke objectFieldOffset and staticFieldOffset. The result is address / location of object in the storage allocation of its class.
Following example program runs well on JDK 1.6
import sun.misc.Unsafe;
import java.lang.reflect.Field;
public class ObjectLocation {
 private static int apple = 10;
 private int orange = 10;
 public static void main(String[] args) throws Exception {
  Unsafe unsafe = getUnsafeInstance();
  Field appleField = ObjectLocation.class.getDeclaredField("apple");
  System.out.println("Location of Apple: "
    + unsafe.staticFieldOffset(appleField));
  Field orangeField = ObjectLocation.class.getDeclaredField("orange");
  System.out.println("Location of Orange: "
    + unsafe.objectFieldOffset(orangeField));
 }
 private static Unsafe getUnsafeInstance() throws SecurityException,
   NoSuchFieldException, IllegalArgumentException,
   IllegalAccessException {
  Field theUnsafeInstance = Unsafe.class.getDeclaredField("theUnsafe");
  theUnsafeInstance.setAccessible(true);
  return (Unsafe) theUnsafeInstance.get(Unsafe.class);
 }
}

Java’s toLowerCase() has got a surprise for you!

Have you ever encountered a surprise while using toLowerCase()? This is a widely used method when it comes to strings and case conversion. There is a nice little thing you should be aware of.
toLowerCase() respects internationalization (i18n). It performs the case conversion with respect to your Locale. When you call toLowerCase(), internally toLowerCase(Locale.getDefault()) is getting called. It is locale sensitive and you should not write a logic around it interpreting locale independently.
import java.util.Locale;
public class ToLocaleTest {
    public static void main(String[] args) throws Exception {
        Locale.setDefault(new Locale("lt")); //setting Lithuanian as locale
        String str = "\u00cc";
    System.out.println("Before case conversion is "+str+" and length is "+str.length());// ÃŒ
        String lowerCaseStr = str.toLowerCase();
    System.out.println("Lower case is "+lowerCaseStr+" and length is "+lowerCaseStr.length());// i?`
    }
}
In the above program, look at the string length before and after conversion. It will be 1 and 3. Yes the length of the string before and after case conversion is different. Your logic will go for a toss when you depend on string length on this scenario. When your program gets executed in a different environment, it may fail. This will be a nice catch in code review.
To make it safer, you may use another method toLowerCase(Locale.English) and override the locale to English always. But then you are not internationalized.
So the crux is, toLowerCase() is locale specific.

Is Your Java Code Privileged?

The java system code that is part of the JDK is considered God and has all the maximum privileges. For example it can read a system property by default. To easily understand it is better to consider java Applets. An Applet cannot read a system property by default because it belongs to different CodeSource and not in same domain as system code. Recall that the system code has all privileges.
Then what do you need to do for Applet to get that privilege? You need to explicitly grant those security privileges by creating a policy file. In that policy you specify what are all the privileges you are granting.
There is another option also. It is opposite of the above. You say that this code doesn’t require any security policy and it is privileged to do the same (anything) as system code. Do you smell something evil here? This is a risky thing to do. Giving away the security is OS dependent. “Privileged code + malicious user + hole in OS” will be a worst thing to tackle.
Therefore you need to keep the code block as minimum as possible, for which you are going to give privilege. You might require this in the following scenarios
  • To read a file
  • To read a system property
  • To create a network connection to the local machine
  • To get direct access to files that contain fonts

How to make java code privileged?

   anyMethod() {
        ...other java code here...
        AccessController.doPrivileged(new PrivilegedAction() {
            public Object run() {
                // put the privileged code here, example:
                System.loadLibrary("awt");
                return null; // in our scenario nothing to return
            }
        });
       ...other code continues...
  }
AccessController API explains more about java privileged code and examples.

Serialize / De-Serialize Java Object From Database

In recent days generalization have become popular in software development. You build a common platform and generate applications out of it to reduce the cost. In such applications an activity that will be frequently performed is serializing java objects to database.
There are many fancy tools and framework available to do this. Before using all of those, you need to understand the low level details of serializing a java object to database.
In older days before the advent of JDBC 3.0 you need to completely rely on streams.
  1. Stream the object to a ByteArrayOutputStream via an ObjectOutputStream.
  2. Convert the ByteArrayOutputStream to a byte array.
  3. Call the setBytes method on the prepared statement.
Now you can use Object type support from jdbc to store the object into database.
There are some key factors before performing serialization.
1. Database in use
2. Datatype to be used to persist the object
Also know some fundamentals of serialization.
I have given a sample java source code below to serialize and de-serialize java object to mysql database. In that, I have commented a line
Object object = rs.getObject(1);
Enable this line and comment the following 4 lines and execute and see the result. You will learn one more point.
To de-serialize a java object from database:
  1. Read the byte array and put it into a ByteArrayInputStream
  2. Pass that to an ObjectInputStream
  3. Then read the object.
Sample Java Source Code – Serialize to DB
Before you run the following example, you need to create a database and a table in it.
Run the following query to create a table before running the sample java program.
I used mysql and driver mysql-connector-java-5.0.3-bin.jar. You may use the database of your choice with tweaks to the connection strings.
create database javaserialization;
CREATE TABLE `serialized_java_objects` (
`serialized_id` int(11) NOT NULL auto_increment,
`object_name` varchar(20) default NULL,
`serialized_object` blob,
PRIMARY KEY  (`serialized_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
public class SerializeToDatabase {
  private static final String SQL_SERIALIZE_OBJECT = "INSERT INTO serialized_java_objects(object_name, serialized_object) VALUES (?, ?)";
  private static final String SQL_DESERIALIZE_OBJECT = "SELECT serialized_object FROM serialized_java_objects WHERE serialized_id = ?";
  public static long serializeJavaObjectToDB(Connection connection,
      Object objectToSerialize) throws SQLException {
    PreparedStatement pstmt = connection
        .prepareStatement(SQL_SERIALIZE_OBJECT);
    // just setting the class name
    pstmt.setString(1, objectToSerialize.getClass().getName());
    pstmt.setObject(2, objectToSerialize);
    pstmt.executeUpdate();
    ResultSet rs = pstmt.getGeneratedKeys();
    int serialized_id = -1;
    if (rs.next()) {
      serialized_id = rs.getInt(1);
    }
    rs.close();
    pstmt.close();
    System.out.println("Java object serialized to database. Object: "
        + objectToSerialize);
    return serialized_id;
  }
  /**
   * To de-serialize a java object from database
   *
   * @throws SQLException
   * @throws IOException
   * @throws ClassNotFoundException
   */
  public static Object deSerializeJavaObjectFromDB(Connection connection,
      long serialized_id) throws SQLException, IOException,
      ClassNotFoundException {
    PreparedStatement pstmt = connection
        .prepareStatement(SQL_DESERIALIZE_OBJECT);
    pstmt.setLong(1, serialized_id);
    ResultSet rs = pstmt.executeQuery();
    rs.next();
    // Object object = rs.getObject(1);
    byte[] buf = rs.getBytes(1);
    ObjectInputStream objectIn = null;
    if (buf != null)
      objectIn = new ObjectInputStream(new ByteArrayInputStream(buf));
    Object deSerializedObject = objectIn.readObject();
    rs.close();
    pstmt.close();
    System.out.println("Java object de-serialized from database. Object: "
        + deSerializedObject + " Classname: "
        + deSerializedObject.getClass().getName());
    return deSerializedObject;
  }
  /**
   * Serialization and de-serialization of java object from mysql
   *
   * @throws ClassNotFoundException
   * @throws SQLException
   * @throws IOException
   */
  public static void main(String args[]) throws ClassNotFoundException,
      SQLException, IOException {
    Connection connection = null;
    String driver = "com.mysql.jdbc.Driver";
    String url = "jdbc:mysql://localhost/javaserialization";
    String username = "root";
    String password = "admin";
    Class.forName(driver);
    connection = DriverManager.getConnection(url, username, password);
    // a sample java object to serialize
    Vector obj = new Vector();
    obj.add("java");
    obj.add("papers");
    // serializing java object to mysql database
    long serialized_id = serializeJavaObjectToDB(connection, obj);
    // de-serializing java object from mysql database
    Vector objFromDatabase = (Vector) deSerializeJavaObjectFromDB(
        connection, serialized_id);
    connection.close();
  }
}

Output of the above java program

Java object serialized to database. Object: [java, papers]
Java object de-serialized from database. Object: [java, papers] Classname: java.util.Vector

Bye bye GoDaddy

It was a bumpy ride in 2009. Many highs and lows. I enjoyed every moment of it. December 2009 is an important month for javapapers. My hosting provider GoDaddy kept me completely busy.
As almost everybody ;-) in the web world I also got my due share of problems from Go Daddy. It all started with trying to host multiple domains using the same hosting account. Economy pack didn’t have multiple hosting option and I was asked to upgrade to next higher plan by paying more bucks. Without spending much time on research I paid the amount. How stupid I was!
What I finally got was a hosting account mapped to one primary domain with all the sub domains visible through the primary one! I raised support requests. I got prompt replies. But I feel like yelling here. All the replies I got are from their robots! Can you imagine they have a software robot to reply for customers. If their robot is that much intelligent they need not be in hosting business. They would be providing software products for NASA.
It just grabs the keywords and responds with same pre-formatted template replies. I tried my BEST to reach a real person and all went in vain. This is what triggered me to look for options. One more issue I was living with from day one is too much advertisement. Their control panel is a crap. To find even a log out link it will take minutes. I was with Godaddy for atleast three years and got used to this interface nightmare.
One more issue (problems list with Godaddy is quite long) was, I am not able to map to a third part DNS server for a domain name. I had a domain registered with them and I was trying to point it to a third party hosting provider. Initially it gets set. But after a week or so (I am not able to find out the window period) they reset it to their own DNS server and a default page, which has tons of garbage advertisements.
I tried couple of times to point it back to my server, but I think they have a cron job to redo it again on a defined interval. This is a serious issue in the hosting world. I know this might be a very odd case but the real problem is I couldn’t get help from their support staff. Finally I was determined to ditch Godaddy and move to a new hosting provider.
This time I did volume of research on hosting providers. Cheap providers are off my list and I had quality as first criteria. I got some three providers. Then finally narrowed down it to mediatemple. Yes it is little heavy on bills, but I heard every penny is worth it. Multiple domain hosting option is a real carrot from them and the clean plesk control panel. I took the bait.
It was a very difficult process from moving the application, data from one hosting provided to another. Main thing is to avoid the downtime in between. You migth blackout and that might cause serious damage to rankings in search engines. I had to go through volume of blog posts and help documents. Finally I got succeeded without any damage.
In transfer of data, mediatemple phpmyadmin’s response is slow. Sometimes even I thought server is down. This is the only issue I got so far. As data import is a very rare job, I am not much worried about it. It is smooth sailing thereafter. Its been a week and there is no incident. We need to wait and see for performance of the mediatemple’s famed servers.
Voila, now javapapers is served to you from mediatemple. Of course with little extra cost for which I got extra quality. I got clean interface to deal with, excellent multiple site hosting option and above all a real person to get support. Do you realize that the pages are rendered quicker by fraction of a second ;-) Bye bye Godaddy.

Java Serialization

Have you ever seen what is inside a serialized object? I will explain you what is java serialization, then provide you with a sample for serialization. Finally most importantly, lets explore what is inside a serialized object and what it means. That is internals of java serialization and how does it works. If you want to have your own implementation of java serialization, this article will provide you with a good platform to launch.

What is Java Serialization?

Primary purpose of java serialization is to write an object into a stream, so that it can be transported through a network and that object can be rebuilt again. When there are two different parties involved, you need a protocol to rebuild the exact same object again. Java serialization API just provides you that. Other ways you can leverage the feature of serialization is, you can use it to perform a deep copy.
Why I used ‘primary purpose’ in the above definition is, sometimes people use java serialization as a replacement for database. Just a placeholder where you can persist an object across sessions. This is not the primary purpose of java serialization. Sometimes, when I interview candidates for Java I hear them saying java serialization is used for storing (to preserve the state) an object and retrieving it. They use it synonymously with database. This is a wrong perception for serialization.

How do you serialize?

When you want to serialize an object, that respective class should implement the marker interface serializable. It just informs the compiler that this java class can be serialized. You can tag properties that should not be serialized as transient. You open a stream and write the object into it. Java API takes care of the serialization protocol and persists the java object in a file in conformance with the protocol. De-serialization is the process of getting the object back from the file to its original form.
Here protocol means, understanding between serializing person and de-serializing person. What will be the contents of file containing the serialized object? This serves as a guideline to de-serialize. Have a look at the following sample and how its serialized file looks.

Sample Source Code for Java Serialization

package com.javapapers.sample;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
class SerializationBox implements Serializable {
  private byte serializableProp = 10;
  public byte getSerializableProp() {
    return serializableProp;
  }
}
public class SerializationSample {
  public static void main(String args[]) throws IOException,
      FileNotFoundException, ClassNotFoundException {
    SerializationBox serialB = new SerializationBox();
    serialize("serial.out", serialB);
    SerializationBox sb = (SerializationBox) deSerialize("serial.out");
    System.out.println(sb.getSerializableProp());
  }
  public static void serialize(String outFile, Object serializableObject)
      throws IOException {
    FileOutputStream fos = new FileOutputStream(outFile);
    ObjectOutputStream oos = new ObjectOutputStream(fos);
    oos.writeObject(serializableObject);
  }
  public static Object deSerialize(String serilizedObject)
      throws FileNotFoundException, IOException, ClassNotFoundException {
    FileInputStream fis = new FileInputStream(serilizedObject);
    ObjectInputStream ois = new ObjectInputStream(fis);
    return ois.readObject();
  }
}

Exploring Java Serialization

Look at following image. After serializing ‘SerializationBox’ in the above sample code, I opened the output in a hex editor. You can use Notepad++ and hex plugin to open the serialized file.
Let us look at contents byte by byte and find out what they are. It starts with “ac ed”. It is is called STREAM_MAGIC. It is a magic number (java API guys says) that is written to the stream header. It denotes that is start of serialzed content.Serialized Output
Similarly every character has a meaning. Actually the serialized file is more bulkier than you would expect, as it has a huge header the meta information of the classes involved and finally the content. Object Serialization Stream Protocol have a look at chapter 6.4.2 Terminal Symbols and Constants. It gives you list of symbols and constants used in serialization.

Decrypting Serialized Java Object

In the image, I have underline a unit of information in a separate color for you to easily identify.
ac ed – STREAM_MAGIC – denotes start of serialzed content
00 05 – STREAM_VERSION – serialization version
73 – TC_OBJECT – new Object
72 – TC_CLASSDESC – new Class Descriptor
00 26 – length of the class name
63 6f 6d 2e 6a 61 76 61 70 61 70 65 72 73 2e 73 61 6d 70 6c 65 2e 53 65 72 69 61 6c 69 7a 61 74 69 6f 6e 42 6f 78 – class name
57 fc 83 ca 02 85 f0 18 – SerialVersionUID
02 – this object is serializable
00 01 – count of properties in the serialzed class – one property in our example
42 00 10 – private byte
73 65 72 69 61 6c 69 7a 61 62 6c 65 50 72 6f 70 78 70 – property name – serializableProp in our example
0a – 10 the value – This is the persisted value of the property in our sample

Java Peer Class

Peer classes are written by java API developers to interface with native objects. Peer classes should be rightly mentioned as peer interfaces. You can program for the interface as if you are connecting to the C / C++ or any native objects. After doing the programming, you can use javah command line tool that is available in your jdk to generate the stub classes.Peer
It will not have any attributes and you cannot inherit them. Those are just a handle for you to write your implementation based on them. Also every peer class is a parallel to a native class and it is a one to many mapping. One peer and many native objects. But mostly it will be used like one to one mapping.
The last sighting of peer classes was in JDK 1.0 (Component) had a method getPeer() which returned a ComponentPeer. But it was immediately deprecated in JDK 1.1 But all those classes are still residing in the JDK and used internally. There is a package java.awt.peer with a complete set of peers for platform dependent objects. You might need to write your own peer classes if you are going to write platform specific code and implementations. If not, just be aware about these peer classes and that’s simply enough.

Java String Concatenation

You have been told many times, don’t use + (java plus operator) to concatenate Strings. We all know that it is not good for performance. Have you researched it? Do you know what is happening behind the hood? Lets explore all about String concatenation now.
In the initial ages of java around jdk 1.2 every body used + to concatenate two String literals. When I say literal I mean it. Strings are immutable. That is, a String cannot be modified. Then what happens when we do
String fruit = "Apple"; fruit = fruit + "World";
In the above java code snippet for String concatenation, it looks like the String is modified. It is not happening. Until JDK 1.4 StringBuffer is used internally and from JDK 1.5 StringBuilder is used to concatenate. After concatenation the resultant StringBuffer or StringBuilder is changed to String.
When java experts say, “don’t use + but use StringBuffer”. If + is going to use StringBuffer internally what big difference it is going to make in String concatenation? Look at the following example. I have used both + and StringBuffer as two different cases. In case 1, I am just using + to concatenate. In case 2, I am changing the String to StringBuffer and then doing the concatenation. Then finally changing it back to String. I used a timer to record the time taken for an example String concatenation.
Look at the output (if you run this java program the result numbers might slightly vary based on your hardware / software configuration). The difference between the two cases is astonishing.
My argument is, if + is using StringBuffer internally for concatenation, then why is this huge difference in time? Let me explain that, when a + is used for concatenation see how many steps are involved:
  1. A StringBuffer object is created
  2. string1 is copied to the newly created StringBuffer object
  3. The “*” is appended to the StringBuffer (concatenation)
  4. The result is converted to back to a String object.
  5. The string1 reference is made to point at that new String.
  6. The old String that string1 previously referenced is then made null.
Hope you understand the serious performance issues and why it is important to use StringBuffer or StringBuilder (from java 1.5) to concatenate Strings.
Therefore you can see initially it was +, then StringBuffer came and now StringBuilder. Surely Java is improving release by release!

Example Java Source Code For String Concatenation

class Clock {
  private final long startTime;
  public Clock() {
    startTime = System.currentTimeMillis();
  }
  public long getElapsedTime() {
    return System.currentTimeMillis() - startTime;
  }
}
public class StringConcatenationExample {
  static final int N = 47500;
  public static void main(String args[]) {
    Clock clock = new Clock();
    //String to be used for concatenation
    String string1 = "";
    for (int i = 1; i <= N; i++) {
      //String concatenation using +
      string1 = string1 + "*";
    }
    //Recording the time taken to concatenate
    System.out.println("Using + Elapsed time: " + clock.getElapsedTime());
    clock = new Clock();
    StringBuffer stringBuffer = new StringBuffer();
    for (int i = 1; i <= N; i++) {
      //String concatenation using StringBuffer
      stringBuffer.append("*");
    }
    String string2 = stringBuffer.toString();
    System.out.println("Using StringBuffer Elapsed time: " + clock.getElapsedTime());
  }
}

Output For The Above Example Program For String Concatenation

Using + Elapsed time: 3687
Using StringBuffer Elapsed time: 16

Formatting Decimal Numbers

DecimalFormat class which is extended from NumberFormat allows you to format a number (both decimal and integer) into a beautified String. It can be used to display in reports in general. DecimalFormat also helps to parse number from String literals.
It can be used in localized application also. That is you can use it based on a particular locale. It is one of the main highlight of this class. You can format a range of numbers like integers (1234), decimal numbers (1234.56), scientific notation (1.23E4), percentages (98%), and currency amounts ($1234).
When formatting a number to a String, first you need to get an instance of the DecimalFormat. Then you can associate a pattern to that formatter. Using this format instance numbers can be formatted. The key here is learning about the pattern symbols available and how they can be used with one another.
Easiest way to learn this is by example. I have given a pretty long example source code to learn formatting. Following are detail of common pattern characters.

Pattern characters for formatting numbers

0 – to signify a digit
# – digit or absence of a number
. – decimal point (you should use always . signify a decimal point in pattern, but the formatted character will be based on Locale
E – separates mantissa and exponent in scientific notation
, – digits group separator
% – multiplies the number by 100 and adds a ‘%’ at the end
‘ – to use a pattern character itself in a pattern. Just a escape character!

Example Java Source Code For Number Formatting

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Locale;
public class DecimalFormatExample {
  public static void main(String args[]) {
    NumberFormat formatter;
    // parsing a negative integer from String
    formatter = new DecimalFormat("#,##0;(#,##0)");
    try {
      System.out.println(formatter.parse("(1,234)"));
    } catch (ParseException e) {
    }
    // formatting to an integer from a decimal
    // with a prefix of 0s
    //uses default RoundingMode RoundingMode.HALF_EVEN
    formatter = new DecimalFormat("00000");
    System.out.println(formatter.format(-123.456));
    // print a example number using different formats
    // for the current Locale
    Locale currentLocale = Locale.getDefault();
    System.out.println("Format based on locale "
        + currentLocale.getDisplayName());
    double exampleNumber = -1234.56;
    for (int j = 0; j < 4; ++j) {
      switch (j) {
      case 0:
        formatter = NumberFormat.getInstance(currentLocale);
        break;
      case 1:
        formatter = NumberFormat.getIntegerInstance(currentLocale);
        break;
      case 2:
        formatter = NumberFormat.getCurrencyInstance(currentLocale);
        break;
      default:
        formatter = NumberFormat.getPercentInstance(currentLocale);
        break;
      }
      String pattern = ((DecimalFormat) formatter).toPattern();
      String formatted = formatter.format(exampleNumber);
      System.out.print(pattern + " -> " + formatted);
      try {
        Number parsedNumber = formatter.parse(formatted);
        System.out.println(" -> parsed back from String to Number "
            + parsedNumber);
      } catch (ParseException e) {
      }
    }
    // setting an explicit + sign for a positive number
    // and using () for negative number
    formatter = new DecimalFormat("+0.0;(0.0)");
    System.out.println(formatter.format(1234.5));
    System.out.println(formatter.format(-1234.5));
  }
}

Output For The Above Example Java Source Code For Number Formatting

-1234
-00123
Format based on locale English (United States)
#,##0.### -> -1,234.56 -> parsed back from String to Number -1234.56
#,##0 -> -1,235 -> parsed back from String to Number -1235
¤#,##0.00;(¤#,##0.00) -> ($1,234.56) -> parsed back from String to Number -1234.56
#,##0% -> -123,456% -> parsed back from String to Number -1234.56
+1234.5
(1234.5)

Java Number Format

When you work in an internationalized application java number formats are going to be a head ache. Always use appropriate tools for its respective task. Though you can use a sledge hammer to open a can it is not meant for that. And there is a high possibility that the can might break.
Formatting
If you are a superficial programmer you may tend to do all the formatting using StringBuilder itself. When it comes to internationalization and localization then it is good to use NumberFomat for all your formatting needs. Otherwise, your numbers will not be unique in all locales.
To use number format, first you have to get a locale instance. Then you can set many attributes of the formatting. Like, you have the option to show comma, restrict the number of decimal places, and set minimum and maximum integer length. If you want to display ‘%’ with respect to locale then you have to use NumberFormat. Just don’t append ‘%’ as a string to your result. Do you want to show paranthesis like (3745) in place of “-” to denote a negative numer, then use NumberFormat. Like these, there are numerous uses. Check javadoc of NumberFormat for more methods.

Convert (Parse) a String to Number

You can use parse to read from a String and convert it to a number. You should get the returned result in Number class as it is the super class of many wrapper classes. Always remember a rule, “program to interface”.
If you are looking for using custom formatter like ####.## and parse numbers accordingly you need to know DecimalFormat. I will write about it in my next post.
import java.text.NumberFormat;
import java.text.ParseException;
public class NumberFormatExample {
  public static void main(String args[]) {
    // get a object for your locale
    NumberFormat numberFormat = NumberFormat.getInstance();
    // setting number of decimal places
    numberFormat.setMinimumFractionDigits(2);
    numberFormat.setMaximumFractionDigits(2);
    // you can also define the length of integer
    // that is the count of digits before the decimal point
    numberFormat.setMinimumIntegerDigits(1);
    numberFormat.setMaximumIntegerDigits(10);
    // if you want the number format to have commas
    // to separate the decimals the set as true
    numberFormat.setGroupingUsed(true);
    // convert from integer to String
    String formattedNr = numberFormat.format(12345678L);
    // note that the output will have 00 in decimal place
    System.out.println("12345678L number formatted to " + formattedNr);
    // convert from decimal to String
    formattedNr = numberFormat.format(12345.671D);
    System.out.println("12345.671D number formatted to " + formattedNr);
    // format a String to number
    Number n1 = null;
    Number n2 = null;
    try {
      n1 = numberFormat.parse("1,234");
      n2 = numberFormat.parse("1.234");
    } catch (ParseException e) {
      System.out.println("I couldn't parse your string!");
    }
    System.out.println(n1);
    System.out.println(n2);
    // show percentage
    numberFormat = NumberFormat.getPercentInstance();
    System.out.println(numberFormat.format(0.98));
  }
}

Output

12345678L number formatted to 12,345,678.00
12345.671D number formatted to 12,345.67
1234
1.234
98%

Java Overflow And Underflow

Overflow and underflow is a condition where you cross the limit of prescribed size for a data type. When overflow or underflow condition is reached, either the program will crash or the underlying implementation of the programming language will have its own way of handing things.
In Java arithmetic operators don’t report overflow and underflow conditions. They simply swallow it! It is a very dangerous thing to do. If one doesn’t know how Java handles overflow and underflow then he will not be aware of things happening behind while doing arithmetic operations.

Overflow and Underflow in Java int operators

Arithmetic integer operations are performed in 32-bit precision. When the resultant value of an operation is larger than 32 bits (the maximum size an int variable can hold) then the low 32 bits only taken into consideration and the high order bits are discarded. When the MSB (most significant bit) is 1 then the value is treated as negative.

Overflow and Underflow in Java floating point operators

While using java floating point operators, overflow will result in Infinity and underflow will result 0.0 As a general rule here also Java doesn’t throw an error or exception for overflow and underflow.

Important points to remember for java overflow and underflow

  1. Overflow or underflow conditions never throw a run time exception
  2. Flowed output is predictable and reproducible. That is, its behaviour is the same every time you run the program.

Example source code for overflow and underflow

public class OverflowUnderflow {
 public static void main(String args[]){
 //roll over effect to lower limit in overflow
 int overflowExample = 2147483647;
 System.out.println("Overflow: "+ (overflowExample + 1));
 //roll over effect to upper limit in underflow
 int underflowExample = -2147483648;
 System.out.println("Underflow: "+ (underflowExample - 1));
 byte b = 127;
 // following line uncommented results in compilation error
 // constants are checked at compile time for size
 // b = b*b;
 double d = 1e308;
 System.out.println(d + "*10= " + d*10);
 //gradual underflow
 d = 1e-305 * Math.PI;
 System.out.print("gradual underflow: " + d + "\n      ");
 for (int i = 0; i < 4; i++)
 System.out.print(" " + (d /= 100000));
 }
}

Output:

Overflow: -2147483648
Underflow: 2147483647
1.0E308*10= Infinity
gradual underflow: 3.141592653589793E-305
 3.1415926535898E-310 3.141592653E-315 3.142E-320 0.0

Overloading vs Overriding

Conceptually overloading and overriding is way different. Only the notion about interface name is same in both cases. Other than that, you cannot find a common thing between them.
Overloading is using the same interface but with different inputs and getting different behaviour as output. Slightly confusing right. I hope by end of this article you may feel better.
Not Overloading
Not Overloading
Overriding is in picture when there is inheritance. When you are inheriting a object there are so many behaviours associated with it. Primarily you are inheriting to use the common behaviour and attributes. But in certain cases, you may not like a particular behaviour. In such cases, you overwrite that behaviour alone in your inherited instance. The interface will be same.
Overloading is not polymorphism. Run time binding is not polymorphism. All these and more are used to exercise the property polymorphism. Polymorphism is a more general property belonging to object-oriented programming parlance and demands a separate article by itself. Let’s discuss that in detail then.

Overloading

Now let’s continue to discuss overloading and overriding. I have shown an image of a overloaded truck. I specifically used this image and drawn a wrong mark on it. Because, when I did some research on this topic using internet almost all web pages where ever overloading is discussed this kind of overloaded truck image is used to explain the meaning. Please get it right. Overloading is not adding more and more attributes and interfaces to the object so that it looks bulkier. In fact, when you use overloading for the outsiders view the object will look compact. That is putting more behaviour with same interface. That is your object will look sleek.
Before Overloading
Before Overloading
I have shown two images, one is Harmonium, a classical music instrument from India. Just for understanding it is a very trimmed version of piano. The other is a modern digital keyboard. Harmonium is without overloading and keyboard is after overloading. In a digital keyboard, keys are the interface, in programming method name. Speaker is the output, in programming return type of the method. When the player presses the keys he gives input to the method and gets output as music through the speakers.
After Overloading
After Overloading
Following line is very important to understand the complete correlation. When different input is given you get different out put. Here the interface (method name, keys) is same and output type is same. But the actual input (arguments, key press sequence) and output (returned data, music) is different. This is overloading. Look at the whole object (keyboard), how sleek it is after overloading.

Example java source code for overloading:

public class OverloadExample {
  public static void main(String args[]) {
    System.out.println(playMusic("C sharp","D sharp"));
    System.out.println(playMusic("C","D flat","E flat"));
  }
  public static String playMusic(String c, String d) {
    return c+d;
  }
  public static String playMusic(String c, String d, String e){
    return c+d+e;
  }
}

Overriding

It is very simple and easy to understand. When you inherit an object, you don’t like certain behaviour and so you replace it with your own. Note the word replace. Because after overriding the old behaviour is completely obsolete. Now look at the image of a monster truck. Here the old small four wheels are replaced with huge wheels to suit the current need. This is overriding.
Overriding
Some specific points about overloading in Java:
A java private method cannot be overridden because in first place it is not accessible to an inheriting object.
Final parameter in the overloaded method, has an interesting behaviour which I leave it for your exercise ;-)

An example for overriding from java package:

Implicitly every object in Java is extended from Object class. Object has a method named equals. This implementation compares the passed object with the current object and returns true if the reference are same. In String class you don’t want this behaviour. Therefore, equals method of the Object class is overridden in String class with its own implementation. Here the behaviour is modified to check if the character sequence is same as the compared one and returns true. This is a classic example of overriding.

Java Clone, Shallow Copy and Deep Copy

Clone (κλών) is a Greek word meaning “branch”, referring to the process whereby a new plant can be created from a twig. In biology it is about copying the DNAs. In real world, if you clone Marilyn Monroe, will you get a copy of her with same beauty and characteristics? No, you will not get! This exactly applies to java also. See how java guys are good at naming technologies.
Marilyn-Monroe
In java, though clone is ‘intended’ to produce a copy of the same object it is not guaranteed. Clone comes with lots of its and buts. So my first advice is to not depend on clones. If you want to provide a handle / method to deliver a copy of the current instance write a kind of factory method and provide it with a good documentation. When you are in a situation to use a third party component and produce copies of it using the clone method, then investigate that implementation carefully and get to know what is underlying. Because when you ask for a rabbit, it may give monkeys!

Shallow Copy

Generally clone method of an object, creates a new instance of the same class and copies all the fields to the new instance and returns it. This is nothing but shallow copy. Object class provides a clone method and provides support for the shallow copy. It returns ‘Object’ as type and you need to explicitly cast back to your original object.
Since the Object class has the clone method (protected) you cannot use it in all your classes. The class which you want to be cloned should implement clone method and overwrite it. It should provide its own meaning for copy or to the least it should invoke the super.clone(). Also you have to implement Cloneable marker interface or else you will get CloneNotSupportedException. When you invoke the super.clone() then you are dependent on the Object class’s implementation and what you get is a shallow copy.

Deep Copy

When you need a deep copy then you need to implement it yourself. When the copied object contains some other object its references are copied recursively in deep copy. When you implement deep copy be careful as you might fall for cyclic dependencies. If you don’t want to implement deep copy yourselves then you can go for serialization. It does implements deep copy implicitly and gracefully handling cyclic dependencies.
One more disadvantage with this clone system is that, most of the interface / abstract class writers in java forget to put a public clone method. For example you can take List. So when you want to clone their implementations you have to ignore the abstract type and use actual implementations like ArrayList by name. This completely removes the advantage and goodness of abstractness.
When implementing a singleton pattern, if its superclass implements a public clone() method, to prevent your subclass from using this class’s clone() method to obtain a copy overwrite it and throw an exception of type  CloneNotSupportedException.
Note that clone is not for instantiation and initialization. It should not be synonymously used as creating a new object. Because the constructor of the cloned objects may never get invoked in the process. It is about copying the object in discussion and not creating new. It completely depends on the clone implementation. One more disadvantage (what to do there are so many), clone prevents the use of final fields. We have to find roundabout ways to copy the final fields into the copied object.
Clone is an agreement between you, compiler and implementer. If you are confident that you all three have good knowledge of java, then go ahead and use clone. If you have a slightest of doubt better copy the object manually.

Example source code for java clone and shallow copy

class Employee implements Cloneable {
 private String name;
 private String designation;
 public Employee() {
 this.setDesignation("Programmer");
 }
 public String getDesignation() {
 return designation;
 }
 public void setDesignation(String designation) {
 this.designation = designation;
 }
 public String getName() {
 return name;
 }
 public void setName(String name) {
 this.name = name;
 }
 public Object clone() throws CloneNotSupportedException {
 /*
 Employee copyObj = new Employee();
 copyObj.setDesignation(this.designation);
 copyObj.setName(this.name);
 return copyObj;
 */
 return super.clone();
 }
}
public class CloneExample {
 public static void main(String arg[]){
 Employee jwz = new Employee();
 jwz.setName("Jamie Zawinski");
 try {
 Employee joel = (Employee) jwz.clone();
 System.out.println(joel.getName());
 System.out.println(joel.getDesignation());
 } catch (CloneNotSupportedException cnse) {
 System.out.println("Cloneable should be implemented. " + cnse );
 }
 }
}
Output of the above program:
Jamie Zawinski
Programmer

Java null and NullPointerException

NullPointerException: An attempt was made to use a null reference in a case where an object reference was required.
NullPointerException is more famous to Java programmers, than fashion to Paris. When you start breaking the beurette and Pipette in Java lab, first thing you will get is NullPointerException. In this tutorial lets discuss about, do we need NullPointerExceptions and why does it comes and how to solve a NullPointerException (NPE). Also we need to study about null, which causes the NullPointerException.
Before going into detail, is NullPointerException named correctly? i think it should have beed name as NullReferenceException as correctly done in dotNet. Hey, I heard there are no pointers in Java atleast for namesake! then how come it is NullPointerException. You might appreciate this after reading the complete story.

Why did NullPointerException became so famous?

Error description for NullPointerException doesn’t tells the name of the null variable, which caused the exception. The programmer, mostly beginner spends lot of time to figure out where the error is. Also it happens so frequently that you get annoyed. Error reported just says the line number at which it occurred. NullPointerException is a RuntimeException.
In a code block, if there is a possibility of null pointer exception it should be checked and reported at compile time. Yes there is language ‘Nice’ which does it and there is no chance of getting a NullPointerException.

Scenarios when we get NullPointerException:

Java API documentation says that NullPointerException can be thrown, when there is an attempt to use null where an object is required. That is you are trying to access a reference where there is no value. Main scenarios are when, calling the instance method of a null object and accessing or modifying the field of a null object. When you pass null to a method when it expects a real value. It means to say that null object is used illegally.
So how do we get a null object in place? When you create an object and not initializing it. When you access a method and it returns a null object.

How to solve a NullPointerException in Java?

It is simple, put a null check! Surround your object with if statement like
Object mayBeNullObj = getTheObjectItMayReturnNull();
if (mayBeNullObj != null) { // to avoid NullPointerException
mayBeNullObj.workOnIt();
}
If you think it is so easy and the subject is done, then you are caught wrong. I think the above method is ugly! Imagine a situation where you work using many third party components (methods), how many if conditions will you put to solve one NullPointerException? It will pollute the code. When you deal with third party components and it is supplied with poor javadoc (it always happens!), there is no other option available but to use this ugly way. If not, you can avoid this by using a better design of software. A simple example is to have proper ‘required field’ check at GUI level, when your business logic expects an operation on a given object. Then you can depend on your design and omit the if-null-check which beefs up your code.
There is a default value for all primitives in Java and it is initialized with it. Why not have something like that for objects? So that null will never exist and the whole world’s NullPointerException problem is solved. Why do we have null in Java? We need it to use in places where there is absense of information. We are depicting real world information in Java programming using OOPS. Zero is absense of information, can I use zero as a replacement for null. No! In mathematics itself zero can be used when there is absense of information, only when you are dealing with numbers. For example when you are working in real algebra, you have a null set not number zero. Of course the size of that is zero ;-) So in real world 0 and absense of existense are not same. Therefore one cannot use empty Sting “” or zero in place of null in java.
Too much techie information for the academics, you may omit this paragraph without loosing integrity of information.
Java Virtual Machine (JVM) specification gives 24 operations that throws NullPointerException. They are aaload, aastore, arraylength, athrow, baload, bastore, caload, castore, daload, dastore, faload, fastore, getfield, iaload, iastore, invokeinterface, invokespecial, laload, lastore, monitorenter, monitorexit, putfield, saload, sastore. Of those 21 would be supplemented by versions that would not throw NullPointerException. The 3 that wouldn’t be supplemented are athrow, monitorenter and monitorexit. 16 of the 21 involve loading or storing the primitive types from or in arrays; 2 involve the same with object references; 2 more: getfield & putfield; 2 for method invocation; and 1 for the length of the array.

Guidelines to solve or deal with NullPointerException:

  • Prune null at design level, and don’t allow the possibilty of having null value in object where the business scenario doesn’t allows.
  • Always declare variables just before where they are going to be used.
  • Always use if-null-check when you don’t have control over the object’s value. (Ugly but no choice!)
    Put the string constant first from left when comparing values with a String variable.
  • Religiously initialize (fill) arrays immediately after declaration. Arrays mostly causes NullPointerException.
  • Return Iterators instead of Lists. This allows to return empty Iterator and the consumer need not do if-null-check. NullPointerException will not creep into this pattern.
  • Erroneously don’t declare a variable inside a constructor, it will hide the instance variable.

Differentiate JVM JRE JDK JIT

Java Virtual Machine (JVM) is an abstract computing machine. Java Runtime Environment (JRE) is an implementation of the JVM. Java Development Kit (JDK) contains JRE along with various development tools like Java libraries, Java source compilers, Java debuggers, bundling and deployment tools.
JVM becomes an instance of JRE at runtime of a java program. It is widely known as a runtime interpreter. The Java virtual machine (JVM) is the cornerstone on top of which the Java technology is built upon. It is the component of the Java technology responsible for its hardware and platform independence. JVM largely helps in the abstraction of inner implementation from the programmers who make use of libraries for their programmes from JDK.
Diagram to show the relations between JVM JRE JDK
Diagram to show the relations between JVM JRE JDK

JVM Internals

Like a real computing machine, JVM has an instruction set and manipulates various memory areas at run time. Thus for different hardware platforms one has corresponding implementation of JVM available as vendor supplied JREs. It is common to implement a programming language using a virtual machine. Historicaly the best-known virtual machine may be the P-Code machine of UCSD Pascal.
A Java virtual machine instruction consists of an opcode specifying the operation to be performed, followed by zero or more operands embodying values to be operated upon. From the point of view of a compiler, the Java Virtual Machine (JVM)is just another processor with an instruction set, Java bytecode, for which code can be generated. Life cycle is as follows, source code to byte code to be interpreted by the JRE and gets converted to the platform specific executable ones.

Sun’s JVM

Sun’s implementations of the Java virtual machine (JVM) is itself called as JRE. Sun’s JRE is availabe as a separate application and also available as part of JDK. Sun’s Java Development Tool Kit (JDK) comes with utility tools for byte code compilation “javac”. Then execution of the byte codes through java programmes using “java” and many more utilities found in the binary directory of JDK. ‘java’ tools forks the JRE. Implementation of JVMs are also actively released by other companies besides Sun Micro Systems.

JVM for other languages

A JVM can also be used to implement programming languages other than Java. For example, Ada source code can be compiled to Java bytecode, which may then be executed by a Java virtual machine (JVM). That is, any language with functionality that can be expressed in terms of a valid class file can be hosted by the Java virtual machine (JVM). Attracted by a generally available, machine-independent platform, implementors of other languages are turning to the Java virtual machine (JVM) as a delivery vehicle for their languages. PHP with Quercus is such an example.

Just-in-time Compiler (JIT)

JIT is the part of the Java Virtual Machine (JVM) that is used to speed up the execution time. JIT compiles parts of the byte code that have similar functionality at the same time, and hence reduces the amount of time needed for compilation. Here the term “compiler” refers to a translator from the instruction set of a Java virtual machine (JVM) to the instruction set of a specific CPU.

Java this Keyword

This definition:
Java’s ‘this’ keyword is used to refer the current instance of the method on which it is used.

Following are the ways to use this

1) To specifically denote that the instance variable is used instead of static or local variable.That is,
private String javaFAQ;
void methodName(String javaFAQ) {
this.javaFAQ = javaFAQ;
}
Here this refers to the instance variable. Here the precedence is high for the local variable. Therefore the absence of the “this” denotes the local variable. If the local variable that is parameter’s name is not same as instance variable then irrespective of this is used or not it denotes the instance variable.
2) This is used to refer the constructors
public JavaQuestions(String javapapers) {
this(javapapers, true);
}
This invokes the constructor of the same java class which has two parameters.

3) This is used to pass the current java instance as parameter
obj.itIsMe(this);
4) Similar to the above this can also be used to return the current instance
CurrentClassName startMethod() {
return this;
}
Note: This may lead to undesired results while used in inner classes in the above two points. Since this will refer to the inner class and not the outer instance.

5) This can be used to get the handle of the current class
Class className = this.getClass(); // this methodology is preferable in java
Though this can be done by, Class className = ABC.class; // here ABC refers to the class name and you need to know that!

As always, this is associated with its instance and this will not work in static methods.

Is javadoc comment a type of standard java comment?

How many types of java comments are there? Everybody knows there are two types of java comments available. Now the misunderstood part starts. What are those two types of java comments?
The (§3.7) Java Language Specification Third Edition says,
  1. Traditional Comment
  2. End of line Comment
What happened to the documentation comment or javadoc comment?
No it is not a type by itself. This is a commonly misunderstood theory in Java. First lets go through the defintion of these two types of java comments as per specification.
1) Traditional Comment
/* text */
Definition: All the text from the ASCII characters /* to the ASCII characters */ is ignored (as in C and C++).
2) End of line Comment
// text
Definition: all the text from the ASCII characters // to the end of the line is ignored (as in C++).
Java comments come with the following properties too:
  • Nested comments are not possible
  • /* and */ have no special meaning in comments that begin with //
  • // has no special meaning in comments that begin with /* or /**
  • And also the lexical grammar implies that comments do not occur within character literals (§3.10.4) or string literals (§3.10.5)
Javadoc Tool
Javadoc tool facilitates the extraction of text added in the java source code between /** and */. The syntax used is /** text */
Now the compiler point of view. When the java compiler encounters /* it ignores the text until it reaches */ Therefore irrespective, it might be /** or /*# or any other character java compiler doesn’t takes that into account. For the java compiler it is a traditional comment.
Therefore as per Java Language Specification Third Edition there is no special type as java documentation comment.

Check list for Internationalization (I18n)

This check list for Internationalization (I18n) will cater to the need of programmers irrespective of the language java /dot net/ j2ee / any other.

Translatable items

  • GUI – Labels and Menu items
  • User messages including third party component generated
  • Log / system generated messages
  • Help – Online help / manual and other deliverable documentation
  • Installation notes / guide / setup information
  • Symbols (Rs., $, …)
  • Icons, Images and Colors
  • Sound
  • Animation

Formatting and Processing items

  • Time, Date, and Calendar
  • Numeric, Monetary, and Metric
  • Personal Names, Honorifics, and Titles
  • Maps / Addresses
  • Layout (Like narrow / broad)
  • Sound-to-Text / speech recognition

Technical

  • Equality checking (String / Character matching)
  • Sorting
  • Text length / wrapping
  • Position / direction of text / objects
  • Encoding Methods
  • Device Input (Keyboard and Input Methods)
  • Device Output (Font Management, Rendering, and Output Methods)

What is scoped memory and why java uses it?

In java, a frequent occurence of a common phenomenon related to memory management is creation and destruction of temporary objects. What are java temporary objects? For example in java, when two strings are concatenated, a StringBuffer object is created. As two Java String objects cannot be added directly (immutable property), a helper object StringBuffer is used to construct a resultant java String object. This StringBuffer object is a temporary object, which is of no direct interest to the programmer. Most of the programming constructs are built in this type of model. Result of it is there are lot of garbage left behind.
Now the question is, what happens to the temporary java object and such garbage after the intended operation completes? Will it be saved for the life time of the program, or JVM (java virtual machine) or what is the underlying policy to destroy it? In automatic memory management the hassles of allocating and deallocating memory is no more a part of programmer’s job in java. But the downside of it is you never know, when an object will be freed. The java garbage collector (GC) doesnot provide any defined time boundaries.
This is where the java scoped memory concept comes into picture. Scoped memory concept is declaring to the JVM that, I am going to use this much amount of memory and those can be freed after this period. That is defining a boundary and bringing predictability. This predictability is needed the most for real time programming. Where you need to be certain about the cycles, time and cost of the operation.
In java scoped memory, the memory is preallocated for the task. This will ensure that the program will not get struck in the mid of operation because of memory resource shortage and constraints. Memory is deallocated and freed when the associated task gets completed. Therefore the recycling process of the memory is fast. But, all the objects related to that task and comes under that designated scope will disappear. If some objects are needed for future reference then that particular object needs to be associated to a different memory mechanism.
This is one of the latest feature in java adding power to the real-time java implementation. For more detail look into the Sun Java Real-Time System (Java RTS). It is Sun’s commercial implementation of the Real-Time Specification for Java (JSR-001).

Java Static Import

First lets understand what does “java import” does to your java program!
Consider the java import statements:
1) import package.ClassA;
2) import package.*;
Java statement (1) gives you a license to use ClassA inside the whole program without the package reference. That is you can use like ClassA obj = new ClassA(); or ClassA.getStatic(); Java statement (2) allows you to use all the java classes that belong the imported package in the above manner. That is without the package reference.
If you don’t use import statement, you can still use the classes of that package. But you should invoke it with package reference whereever you use.
That is like, package.ClassA obj = new package.ClassA(); – looks very ugly isn’t it?
Now coming to the static import part. Like the above ugly line we have been unknowingly using (abusing) the java static feature.
Consider the java example: double r = Math.cos(Math.PI * theta);
How about writing the same java code like: double r = cos(PI * theta); – looks more readable right?
This is where static import in java comes to help you.
import static java.lang.Math.PI;
import static java.lang.Math.cos;
Do the above static imports and then you can write it in the more readable  way!

Java Static Import

The normal import declaration imports classes from packages, so that they can be used without package reference. Similarly the static import declaration imports static members from classes and allowing them to be used without class reference.
Now, we have got an excellent java feature from java 1.5. Ok now we shall see how we can abuse this!

Can i static import everything?

like, import static java.lang.Math.*; – yes it is allowed! Similarly you do for class import.
Please don’t use this feature, because over a period you may not understand which static method or static attribute belongs to which class inside the java program. The program may become unreadable.
General guidelines to use static java import:
1) Use it to declare local copies of java constants
2) When you require frequent access to static members from one or two java classes

Java Variable

Java variables can be categorized into the following seven types:
  1. Class Variable
  2. Instance Variable
  3. Array Component Variable
  4. Method Parameter Variable
  5. Constructor Parameter Variable
  6. Exception Handler Parameter Variable
  7. Local Variable
1) Class Variable
A java class variable is a field declared using the keyword static within a java class, or with or without the keyword static within a java interface declaration.
2) Instance Variable
Java variables that are declared without static keyword are instance variables.
3) Array Component
Array components are unnamed java variables that are created and initialized to default values whenever a new java array object is created.
4) Method Parameter
Java variables declared in the method declaration signature are method parameter variables. Whenever a java method is invoked a variable is created in the same name as it is declared.
5) Constructor Parameter
This is similar to the java method parameter variable. The same way, for all the java variables declared in the constructor a variable is created whenever it is invoked.
6) Exception Handler Parameter
Java variables that are declared in the catch clause of a java exception handling mechanism. Whenever a java exception is caught, exception handler parameter variable is created.
7) Local Variable
Java variables that are declared in a block inside a java method or for loop is called a java local variable.
Reference: Java Language Specification 4.12.3

Java Final Keyword

  • A java variable can be declared using the keyword final. Then the final variable can be assigned only once.
  • A variable that is declared as final and not initialized is called a blank final variable. A blank final variable forces the constructors to initialise it.
  • Java classes declared as final cannot be extended. Restricting inheritance!
  • Methods declared as final cannot be overridden. In methods private is equal to final, but in variables it is not.
  • final parameters – values of the parameters cannot be changed after initialization. Do a small java exercise to find out the implications of final parameters in method overriding.
  • Java local classes can only reference local variables and parameters that are declared as final.
  • A visible advantage of declaring a java variable as static final is, the compiled java class results in faster performance.

A discussion inviting controversy on java final keyword:

‘final’ should not be called as constants. Because when an array is declared as final, the state of the object stored in the array can be modified. You need to make it immutable in order not to allow modifcations. In general context constants will not allow to modify. In C++, an array declared as const will not allow the above scenario but java allows. So java’s final is not the general constant used across in computer languages.
A variable that is declared static final is closer to constants in general software terminology. You must instantiate the variable when you declare it static final.
Definition as per java language specification (third edition) – 4.12.4 is “A final variable may only be assigned to once.”(§4.1.2)
Java language specification tries to redefine the meaning of constant in the following way!
We call a variable, of primitive type or type String, that is final and initialized with a compile-time constant expression (§15.28) a constant variable. Whether a variable is a constant variable or not may have implications with respect to class initialization (§12.4.1), binary compatibility (§13.1, §13.4.9) and definite assignment (§16).

Access Modifiers In Java

Access modifiers specifies who can access them. There are four access modifiers used in java. They are public, private, protected, no modifer (declaring without an access modifer). Using ‘no modifier’ is also sometimes referred as ‘package-private’ or ‘default’ or ‘friendly’ access. Usage of these access modifiers is restricted to two levels. The two levels are class level access modifiers and member level access modifiers.

I) Class level access modifiers (java classes only)

Only two access modifiers is allowed, public and no modifier
  • If a class is ‘public’, then it CAN be accessed from ANYWHERE.
  • If a class has ‘no modifer’, then it CAN ONLY be accessed from ‘same package’.

II) Member level access modifiers (java variables and java methods)

All the four public, private, protected and no modifer is allowed.
  • public and no modifier – the same way as used in class level.
  • private – members CAN ONLY access.
  • protected – CAN be accessed from ‘same package’ and a subclass existing in any package can access.
For better understanding, member level access is formulated as a table:

Access Modifiers

Same Class Same Package Subclass Other packages
public Y Y Y Y
protected Y Y Y N
no access modifier Y Y N N
private Y N N N
First row {public Y Y Y Y} should be interpreted as:
  • Y – A member declared with ‘public’ access modifier CAN be accessed by the members of the ‘same class’.
  • Y – A member declared with ‘public’ access modifier CAN be accessed by the members of the ‘same package’.
  • Y – A member declared with ‘public’ access modifier CAN be accessed by the members of the ‘subclass’.
  • Y – A member declared as ‘public’ CAN be accessed from ‘Other packages’.
Second row {protected Y Y Y N} should be interpreted as:
  • Y – A member declared with ‘protected’ access modifier CAN be accessed by the members of the ‘same class’.
  • Y – A member declared with ‘protected’ access modifier CAN be accessed by the members of the ‘same package’.
  • Y – A member declared with ‘protected’ access modifier CAN be accessed by the members of the ‘subclass’.
  • N – A member declared with ‘protected’ access modifier CANNOT be accessed by the members of the ‘Other package’.
similarly interpret the access modifiers table for the third (no access modifier) and fourth (private access modifier) records.

Java Static

Java Static Variables

  • Java instance variables are given separate memory for storage. If there is a need for a variable to be common to all the objects of a single java class, then the static modifier should be used in the variable declaration.
  • Any java object that belongs to that class can modify its static variables.
  • Also, an instance is not a must to modify the static variable and it can be accessed using the java class directly.
  • Static variables can be accessed by java instance methods also.
  • When the value of a constant is known at compile time it is declared ‘final’ using the ‘static’ keyword.

Java Static Methods

  • Similar to static variables, java static methods are also common to classes and not tied to a java instance.
  • Good practice in java is that, static methods should be invoked with using the class name though it can be invoked using an object. ClassName.methodName(arguments) or objectName.methodName(arguments)
  • General use for java static methods is to access static fields.
  • Static methods can be accessed by java instance methods.
  • Java static methods cannot access instance variables or instance methods directly.
  • Java static methods cannot use the ‘this’ keyword.

Java Static Classes

  • For java classes, only an inner class can be declared using the static modifier.
  • For java a static inner class it does not mean that, all their members are static. These are called nested static classes in java.

Difference between static and non-static java inner class.

A static java inner class cannot have instances. A non-static java inner class can have instances that belong to the outer class.

Can a java subclass declare a private method available in its java super class?

Yes. A java private member cannot be inherited as it is available only to the declared java class. Since the private members cannot be inherited, there is no place for discussion on java runtime overloading or java overriding (polymorphism) features.

Java Pass By Value and Pass By Reference.

Pass by value in java means passing a copy of the value to be passed. Pass by reference in java means the passing the address itself. In Java the arguments are always passed by value. Java only supports pass by value.
With Java objects, the object reference itself is passed by value and so both the original reference and parameter copy both refer to the same Java object. Java primitives too are passed by value.

Can an unreachable Java object become reachable again?

Yes. It can happen when the Java object’s finalize() method is invoked and the Java object performs an operation which causes it to become accessible to reachable objects.

Java finalization

Finalization is to give an unreachable Java object the opportunity to perform any cleanup processing before the Java object is garbage collected. It happens when the Java object’s finalize() method is invoked.

Java garbage collection

Garbage collection in Java is to discard objects that are no longer needed and to reclaim their resources. A Java object is subject to garbage collection when it is out of scope of the control flow of the program.

Does Java garbage collection guarantee that a program will not run out of memory?

No. Java Programs may use up memory resources faster than they are garbage collected. A Java program can create objects that are not subject to garbage collection.





Ajax

Getting Started with AJAX using Java

21/07/2010
AJAX is an acronym for Asynchronous JavaScript And XML. AJAX provides an ability to communicate with the server asynchronously. Here asynchronous is the keyword. To explain that in simple terms, you can send a request to server and continue user interaction with the user. You need not wait for response from the server. Once the response arrives, a designated area in UI will update itself and reflect the response information. Whole page need not be reloaded.
This is achieved by AJAX using XMLHttpRequest object. Your browser provides the capability for XMLHttpRequest object. Most modern browsers provides support for XMLHttpRequest. This object helps for http request and process XML response. It is not mandatory that you should use only XML. Simple text can also be used in Ajax but which is uncommon.
Before continuing with the article, I assume that you have basic knowledge about http headers, request response mechanism, different method types and response codes. If you lack knowledge in these areas, it is better to update them before proceeding. If you cant read GET, POST, HTTP status 200 OK and response Content-Type: text/html, xml then you must know these topics before learning AJAX. I am not writing in detail about them here, because each one of them calls for a detailed separate article.
Let me write a HelloWorld ajax web application to demonstrate basics. We shall have a button with name ‘Say Hello!’ On click of that button, without reloading the whole page we will display “Hello World!” by replacing the ‘Say Hello!’ button. Following source code listing contains complete code of sample web application.

index.jsp

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>Getting Started with AJAX using JAVA</title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <script type="text/javascript" language="javascript" src="ajax.js"></script>
</head>
<body>
  <div>Getting Started with AJAX using JAVA: Hello World!</div>
  <div id="hello"><button type="button" onclick="makeRequest()">Say Hello!</button></div>
</body>
</html>
index.jsp contains a div ‘hello’. That is the div which XMLHttpRequest object is going to overwrite with response from Servlet. On click of the button we call a java script function makeRequest(). Until now, there is nothing special. Its usual jsp and javascript call. Ajax is not in the picture.
Now go through makeRequest() given below. Inside that we call getXMLHttpRequest() which returns a XMLHttpRequest object. That can be used as a utility method in all your AJAX programs. Thats an attempt to standardization. Different versions of browsers provide different ways of creating XMLHttpRequest. We are covering all possible combinations inside that method.
Once we get XMLHttpRequest object, we need to register a function which will be called on state change. Now its time to explain in detail about XMLHttpRequest object.

XMLHttpRequest properties and events

XMLHttpRequest consists of properties readyState, status, statusText, responseText and responseXML.
  • readyState denotes states as 0 – UNINITIALIZED, 1 – LOADING, 2 – LOADED, 3 – INTERACTIVE, 4 – COMPLETE.
  • status is HTTP status code for the response
  • statusText is HTTP status message for the status code
  • responseText is response text from server
  • responseXML is DOM document object of reponse XML document from server
XMLHttpRequest contains an event ‘onreadystatechange’. It is invoked whenever ‘readyState’ property given above changes.
We need to register a function for the above event ‘onreadystatechange’. In our makeRequest(), after getting xmlHttpRequest object we register getReadyStateHandler(xmlHttpRequest). Therefore whenever there is a state change, this function will be called by the XMLHttpRequest / browser.
After registering the callback funtion we set the request url as the HelloWorld servlet. In web.xml we have done the servlet mapping for that servlet.
In getReadyStateHandler function, if readyState is 4 and http status code is 200 then we set the reponse text from XMLHttpRequest object to the div hello in index.jsp.

ajax.js

/*
 * creates a new XMLHttpRequest object which is the backbone of AJAX,
 * or returns false if the browser doesn't support it
 */
function getXMLHttpRequest() {
  var xmlHttpReq = false;
  // to create XMLHttpRequest object in non-Microsoft browsers
  if (window.XMLHttpRequest) {
    xmlHttpReq = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    try {
      // to create XMLHttpRequest object in later versions
      // of Internet Explorer
      xmlHttpReq = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (exp1) {
      try {
        // to create XMLHttpRequest object in older versions
        // of Internet Explorer
        xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (exp2) {
        xmlHttpReq = false;
      }
    }
  }
  return xmlHttpReq;
}
/*
 * AJAX call starts with this function
 */
function makeRequest() {
  var xmlHttpRequest = getXMLHttpRequest();
  xmlHttpRequest.onreadystatechange = getReadyStateHandler(xmlHttpRequest);
  xmlHttpRequest.open("POST", "helloWorld.do", true);
  xmlHttpRequest.setRequestHeader("Content-Type",
      "application/x-www-form-urlencoded");
  xmlHttpRequest.send(null);
}
/*
 * Returns a function that waits for the state change in XMLHttpRequest
 */
function getReadyStateHandler(xmlHttpRequest) {
  // an anonymous function returned
  // it listens to the XMLHttpRequest instance
  return function() {
    if (xmlHttpRequest.readyState == 4) {
      if (xmlHttpRequest.status == 200) {
        document.getElementById("hello").innerHTML = xmlHttpRequest.responseText;
      } else {
        alert("HTTP error " + xmlHttpRequest.status + ": " + xmlHttpRequest.statusText);
      }
    }
  };
}
A simple Hello World servet sending response as Hello World! as text.

HelloWorld.java

package com.javapapers.sample.ajax;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HelloWorld extends HttpServlet {
  /**
   * A simple HelloWorld Servlet
   */
  public void doPost(HttpServletRequest req, HttpServletResponse res)
      throws java.io.IOException {
    res.setContentType("text/html");
    res.getWriter().write("Hello World!");
  }
  public void doGet(HttpServletRequest req, HttpServletResponse res)
      throws java.io.IOException {
    doPost(req, res);
  }
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>Getting Started with AJAX using JAVA</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>helloWorld</servlet-name>
    <servlet-class>com.javapapers.sample.ajax.HelloWorld</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>helloWorld</servlet-name>
    <url-pattern>/helloWorld.do</url-pattern>
  </servlet-mapping>
</web-app>

Output of Hello World AJAX

Before AJAX call:

After AJAX call:

I have used text response to demonstrate AJAX. There is a lot more to AJAX. I will get into using XML, advanced techniques and jquery in future articles.

Saturday, May 7, 2011

Online Cricket Watch link

http://www.andhrakings.net/t3218-ipl-official-live-link#9029