Monday, December 13, 2010

JDK7 and Java SE7

Most of all are knowing about the Java 7 is planned to release on mid 2011. This JDK will have new feature which all we can categorize in these : Productivity, Performance, Universality, Modularity Integration, Serviceability.

In the Productivity area, we should expect to see new features like simplified Generics, which allows for easier code writing by not having to repeat the generic part both in the declaration of the variable and in the instantiation of the object. There's also the new try syntax for using resources, which allows you to declare temporal resources that need to be closed after the try block is done.

In the performance area, there are several things that come for the new version, which include the Fork/Join Framework, in which you can take a long running process and divide it in several threads, which is done automatically by the framework. Also, they introduced the concept of Lambda expressions, which is Java's way of implementing closures, combined with default methods for Interface declarations to allow for the expressions simplification. They also introduced the concept of reification, value classes and the long awaited properties.

In the universality area, they introduced a new project, DaVinci, which will allow first-class architectural support for languages other than java, like Ruby and Python.

Modularity integration, which they call Project Jigsaw, is probably one of the coolest features to me, and what they're trying to do is take away the classpath "hell" in which you have to define every single jar on the command line, and instead you create modules, and use a special module definition language where you specify the dependencies. New tools are introduced for this, jpkg which creates modules, and jmod, which allows for the management of the modules. When you create a new module, you install it in a local repository. As for the dependencies, you can add external module repositories so that the dependencies are downloaded and installed automatically. They even integrate with Maven, which is great because there are already lots of resources in the different maven repositories. There's also a new package format, jmod, which is supposed to be better for compressing java class files, and they will even support the generation of rpm (one of Linux package formats) natively. This is more a less extensive modularity feature of new Java.

Finally, in the Serviceability area, something called JVM Convergence, in which they want to extend the concept of a jvm make it a black box, which means you will be running a virtual machine that might be composed of one or more real machines in the background.

As for the release schedule, it seems like they are going with plan B. In case you didn't know, Mark Reinhold commented on his blog the current release schedule and how it was unrealistic. Some of the features have been finished, like Project Coin (in part) , InvokeDynamic (dynamic language support) and Fork/Join Framework and some features that aren't finished like Jigsaw (modules) and Lambda (closures). The idea is to release the completed features mid 2011, and move the rest of them to Java 8, to be released (hopefully) Late 2012.

More information about this can be found at the main JDK 7 site.

Sunday, November 21, 2010

Iterator vs Enumeration

As all of you know both Iterator and Enumeration are used to traverse Collection objects, in a sequential fashion. Enumeration can be applied to Vector and HashTable. Iterator can be used with most of the Collection objects.

Differences between Iterator & Enumeration:
Enumeration is twice as fast as Iterator and uses very less memory. Enumeration is very basic and fits to basic needs. But Iterator is much safer as compared to Enumeration, b’ coz it always denies other threads to modify the collection object which is being iterated by it. Whenever a second thread tries for that Iterator will throw a ConcurrentModificationException. Iterators that do this are known as fail-fast iterators, as they fail quickly and cleanly
.

See what happens to this code segment,

Vector
aVector = new Vector();
aVector.add("I");
aVector.add("am");
aVector.add("really");
aVector.add("good");
Enumeration
anEnum = aVector.elements();
Iterator
anItr = aVector.iterator();
// Traversal using Iterator
while(anItr.hasNext())
{
if ()
// This statement will throw ConcurrentModificationException.
// Means, Iterator won't allow object modification while it is
// getting traversed. Even in the same thread.

aVector.remove(index);

System.out.println(anItr.next());
}
// Traversal using Enumeration
while(anEnum.hasMoreElements())
{
if ()
aVector.remove(index);

System.out.println(anEnum.nextElement());
}

But Iterator provides a safer way to remove elements from the underlying collection during the iteration with well-defined semantics. See the implementation of Iterator. But here the remove() is supported by only those implementations of Collection that supports element removal.

public interface Iterator
{
boolean hasNext();
Object next();
void remove(); // Optional
}
So the above program part can be re-writen as,

while(anItr.hasNext())
{
System.out.println(anItr.next());
if ()
anItr.remove();
// Note:
// Before using anItr.remove(), the Iterator should
// point to any of its elements. The remove() removes the
// element which the Iterator corrently pointing to.
// Otherwise it will throw IllegalStateException


}

Note that Iterator.remove() is the only safe way to modify a collection during iteration. In Enumeration, there is “no safe way” to remove elements from a collection while traversing.

Thursday, October 28, 2010

AutoBoxing in java : A boon when used carefully!

I am sure this is a very known mistake that should be avoided, but, it costed me an hour to figure out why the application I am working on was not behaving appropriately. I was looping through a list to find out an object that has to be removed. And, then the index was to be used to remove the object. The Objects in the list had over-ridden the equals and hashcode method,and I could not use the 'indexOf' or 'remove' on the list to find the object and remove it. My mistake was to use Object Integer instead of native int. Well every thing worked fine, but When called remove on the list, the List was trying to remove the integer object instead of object at that integer index! Solution was simple as you al know!! Just an interesting point to be taken down in my diary.

private List stocksSubscribed = new ArrayList();

private void removeStockWithStockName(String symbol) {
Integer removalIndex = 0;
for(Stock stock : stocksSubscribed){
if(symbol.equalsIgnoreCase(stock.getSymbol())){
break;
}
removalIndex++;
}
stocksSubscribed.remove((int)removalIndex);
}

Thursday, July 15, 2010

Our men in uniform are fighting the nation's battle. We should not demoralise them

Anybody who has been watching the Kashmir tragedy unfold on live television will know who's the aggressor, who's on the defensive. The mobs are running riot, provoking, attacking and bullying the police and armed forces. The latter are withdrawing into their shells, trying to defend themselves as best as they can. If we still hear about civilian deaths daily, it's not too difficult to imagine why: when the attackers get too close or threatening, the men in uniform fire, and someone, sometimes drops dead. It's not really about a trigger-happy force.

This is not the time to demoralise our men in uniform. They are fighting the nation's battles in extremely adverse conditions. Our politicians keep screwing up continuously, and we then send in the forces to bring back some semblance of order. But the human rights rabble-rousers would like us to believe that our armed forces are little more than criminals. This is completely unacceptable.

There is more rape and murder in lawless Delhi than in Kashmir. But we want to paint the armed forces as the villains of Kashmir. Libertarians need to think about the rights of uniformed Indians as much as civilians.
Let's be clear why the armed forces are there in the first place. Kashmir is where embattled secularism is trying to hold its o...

Thursday, June 3, 2010

Expectations and Disappointments...

I have faced a cycle of this always in reference to my relationships with others. People's expectations from me, and their opinion of my expectations from them is so different from what I believe. I have always been a person who will call you or contact you when I truly feel like, not when I feel that it's a courtesy to call. And, I expect the same from you. I expect that people who are close to me will not only "Call" me whenever they require me for their purpose only . I don't remember if I ever myself complaint anybody about being too busy to call [Exceptions are always there ;) ]. I have friends who does and those relationships are/will not so strong[failed] for me.

May be I am too unsuited for what a "social" relationship means today's practical & professional era. I probably cannot change myself, because whenever I tried to, I felt as if I am "faking" to be something which I am not.... But, I do force myself occasionally, because there are some situation where I cannot afford to make others changes, instead of change myself. But it happens occasionally for small span and very hard do ...... :(

Sunday, January 17, 2010

My first blog!

And what is all this fuss about! My first love, My first ball, My first date, My first kiss... , or ...................... "My first blog"! Sounds stupid.
Anyways, I never thought blogging was a very good idea, except if you want to do some tik-tak on the keyboard even in your free time!!
But still, here I am, with "My first blog"!