Category Archives: Technology

What’s new in Java 1.7?

First thing first. To determine what set of small language changes should be added to JDK 7, a project has been set up called Project Coin. The final five changes (bit more than 5) are described on the following blog entry of Joseph D. Darcy.

So what made it through is the following:

  • Strings in switch
  • Automatic Resource Management
  • Improved Type Inference for Generic Instance Creation (diamond)
  • Simplified Varargs Method Invocation
  • An omnibus proposal for better integral literals
  • Language support for Collections
  • Language support for JSR 292

There is a list of other features available on the OpenJDK 7 features page.

These features are divided in different categories:

VM

  • Compressed 64-bit object pointers
  • Garbage-First GC (G1)
  • JSR 292: VM support for non-Java languages (InvokeDynamic)

lang

  • SR 294: Language and VM support for modular programming
  • JSR 308: Annotations on Java types
  • JSR TBD: Small language enhancements (the Project Coin I was talking about)
  • JSR TBD: Project Lambda

core

  • Modularization (Project Jigsaw)
  • Upgrade class-loader architecture
  • Method to close a URLClassLoader
  • Unicode 5.1
  • Concurrency and collections updates (jsr166y)
  • JSR 203: More new I/O APIs for the Java platform (NIO.2)
  • SCTP (Stream Control Transmission Protocol)
  • SDP (Sockets Direct Protocol)
  • Elliptic-curve cryptography (ECC)

client

  • XRender pipeline for Java 2D
  • Forward-port 6u10 deployment features
  • Create new platform APIs for 6u10 graphics features
  • Nimbus look-and-feel for Swing
  • Swing JLayer component

web

  • Update the XML stack

As you can see there is a lot of stuff. I personally tried the new Garbage Collector (G1) a few months back and was really impressed by the performance. Unfortunately the JVM was crashing every few hours so it couldn’t be used for production. This GC is available in Java 1.6 as well but same thing, it crashes every so often.

I think that’s it for the new features. Maybe it’s a good idea to see some code examples now.

Code examples for new features in Java 1.7

Most of what is below come from the excellent article from Joe Wright on his blog about New language features in Java 7

Language support for collections

This is all about writing less code when you create a List, a Set or a Map. You don’t have to instantiate the Object and then add the element to the Collection. You can now do it in 1 line.

List<String> list = [“item”];
String item = list[0];

Set<String> set = {“item”};

Map<String, Integer> map = {“key” : 1};
int value = map[“key”];

Automatic Resource Management

Annoyed to have verbose code because of try / catch statement. You will love this one.

Indeed, this:

BufferedReader br = new BufferedReader(new FileReader(path));
try {
return br.readLine();
} finally {
br.close();
}

Become this:

try (BufferedReader br = new BufferedReader(new FileReader(path)) {
return br.readLine();
}

Improved Type Inference for Generic Instance Creation (diamond)

Same thing, today you specify the Generic when you declare the interface of your object and then you have to repeat yourself when you instantiate the object. You won’t have to now as you can do:

Map<String, List<String>> map = new HashMap<>();

Underscores in numeric literals

Not sure this one will be useful to lot of people. You can do:

int billion = 1_000_000_000;

Strings in switch

Nothing to explain here, the title says it all.

String availability = “available”;
switch(availability) {
case “available”:
//code
break;

case “unavailable”:
//code
break;

case “merged”:
//code

default:
//code
break;
}

Binary literals

You can create binary literals using the 0b prefix

int binary = 0b1001_1001;

That’s it for the code examples. It would be great if someone can point me on more things. I am sure there are plenty of other cool stuff.

Performance of Java 1.7

I have picked up the tests I ran from an article from Taranfx posted here.

So the tests are the following (my code, not his but I followed the same ideas). I ran all of this on a Macbook Pro running ArchLinux (the one with an Intel(R) Core(TM)2 Duo CPU T7700 @ 2.40GHz. I think 2 years old). I have 2Gb of RAM. I set the Heap Size to 728m (-Xms728m -Xmx728m).

Test 1. Add 1 Million String values to a List (the String is a UUID generated using UUID.randomUUID()).

Test 2. HashMap with 1 million keys, values. Each key, value pair is being calculated via concurrent thread. The key is a UUID and the int is generated using Math.random()

Test 3. Printing 1 million items of ArrayList to number of Files (1000). Writing to different files happening in parallel.

I am just comparing Java 1.6 (1.6.0_19) vs Java 1.7 (b87). I added Java 1.5 to this benchmark following a request in the comments but not Java 1.4 as it is from an other age to me.

So here is the result

Performance benchmark Java 1.5 vs Java 1.6 vs Java 1.7

Java 1.5 Java 1.6 Java 1.7
Test 1 10,698 sec 9,481 sec 9,328 sec
Test 2 69,827 sec 37,935 sec 36,636 sec
Test 3 26,931 sec 30,868 sec 27,383 sec

The difference of performance is clearly not as big as it is in the article of Taranfx. It appears that our implementation of his tests are probably quite different as well as my tests are taking a lot more time than his.

Release date of Java 1.7

In November 2009, it was planned to be in September 2010 as there would be 3 more milestones. Milestone 6 was completed with build 84. b85 was scheduled for the 4th of March 2010, first build of Milestone 7. b87 used in this article has been released the 25th of March 2010. So it looks like a release for September 2010 is more than likely. Though I have been reading an interesting subject on the stackoverflow forum here. You can check the blog of Alex Miller as well. He publishes links to all blog and news items referring to the progress of Java7.

Thanks

http://inebium.com/post/java-7-new-release-performance-code

Zing Chart

Build Flash or HTML5 charts with the latest & greatest features and technologies

  • More than a Dozen Chart Types
  • Handles Massive Data Sets (10,000 points and more)
  • Fly thru Chart Data with Zooming, Scrolling, and Filtering
  • Build Interactive and Drillable Graphs
  • Live Data Feed Support to Update Charts in Realtime

Compare Canvas based charting with Flash one here http://www.zingchart.com/flash-and-html5-canvas/#speedtest

Download free trial @ : http://www.zingchart.com/download/

Abstract Factory Pattern – Creational Patterns

Download Sample Here

The Abstract Factory pattern is one level of abstraction higher than the factory pattern. You can use this pattern when you want to return one of several related classes of objects, each of which can return several different objects on request. In other words, the Abstract Factory is a factory object that returns one of several factories.

One classic application of the abstract factory is the case where your system needs to support multiple “look-and-feel” user interfaces, such as Windows-9x, Motif or Macintosh. You tell the factory that you want your program to look like Windows and it returns a GUI factory which returns Windows-like objects. Then when you request specific objects such as buttons, check boxes and windows, the GUI factory returns Windows instances of these visual interface components.

In Java 1.2 the pluggable look-and-feel classes accomplish this at the system level so that instances of the visual interface components are returned correctly once the type of look-and-feel is selected by the program. Here we find the name of the current windowing system and then tell the PLAF abstract factory to generate the correct objects for us.

String laf = UIManager.getSystemLookAndFeelClassName();

try {

UIManager.setLookAndFeel(laf);

}

catch (UnsupportedLookAndFeelException exc){

System.err.println(“UnsupportedL&F: ” + laf);

}

catch (Exception exc){

System.err.println(“Error loading ” + laf);

}

This pattern is one level of abstraction higher than factory pattern. This means that the abstract factory returns the factory of classes. Like Factory pattern returned one of the several sub-classes, this returns such factory which later will return one of the sub-classes.

Let’s understand this pattern with the help of an example given in the sample code. Suppose we need to get the specification of various parts of a computer based on which work the computer will be used for.

The different parts of computer are, say Monitor, RAM and Processor. The different types of computers are PC, Workstation and Server.  So, here we have an abstract base class Computer. This class, as you can see, has three methods all returning different parts of computer. They all return a method called Parts. The specification of Parts will be different for different types of computers. Let’s have a look at the class Parts. And now let’s go to the sub-classes of Computer. They are PC, and Server.

When to use Abstract Factory Pattern?

One of the main advantages of Abstract Factory Pattern is that it isolates the concrete classes that are generated. The names of actual implementing classes are not needed to be known at the client side. Because of the isolation, you can change the implementation from one factory to another.

Download Sample Here