Friday, July 17, 2009

WHAT'S NEW IN JAVA

DOJO

Dojo Toolkit is an open source modular JavaScript library (or more specifically JavaScript toolkit) designed to ease the rapid development of cross platform for JavaScript/Ajax based applications and web sites.
Dojo is an Open Source DHTML toolkit written in JavaScript, it will support Cross-platforms present stable Dojo release is 1.3.2.
Dojo Base includes Ajax, events, packaging, CSS-based querying, animations, JSON, language utilities and that's just the beginning. Dojo aims to solve some long-standing historical problems with DHTML which prevented mass adoption of dynamic web application development.

Dojo allows you to easily build dynamic capabilities into web pages and any other environment that supports JavaScript sanely. You can use the components that Dojo provides to make your web sites more usable, responsive, and functional. With Dojo you can build degradable user interfaces more easily, prototype interactive widgets quickly, and animate transitions. You can use the lower-level APIs and compatibility layers from Dojo to write portable JavaScript and simplify complex scripts. Dojo's event system, I/O APIs, and generic language enhancement form the basis of a powerful programming environment. You can use the Dojo build tools to write command-line unit-tests for your JavaScript code. The Dojo build process helps you optimize your JavaScript for deployment by grouping sets of files together and reuse those groups through "profiles".

Dojo does all of these things by layering capabilities onto a very small core which provides the package system and little else. When you write scripts with Dojo, you can include as little or as much of the available APIs as you need to suit your needs. Dojo provides multiple points of entry, interpreter independence, forward looking APIs, and focuses on reducing barriers to adoption.

Dojo Features

Asynchronous communication,
Packaging system,
Client-side data storage,
Server-side data storage,
Support for Adobe Integrated Runtime (AIR).
For More information please click

Announcements

James Gosling: on the Java Road

This is update regarding on 2009 JVM Language Summit to be held at Sun's Santa Clara campus on September 16-18, 2009. More information is available at http://jvmlangsummit.com.

The JVM Language Summit is an open technical collaboration among language designers, compiler writers, tool builders, runtime engineers, and VM architects. We will share our experiences as creators of programming languages for the JVM and of the JVM itself.

By - Ramesh

PRODUCT AND TECHNOLOGY RELEASES

Oracle Database 11g

New for Java, JDBC, .NET, PHP, and OCI

For JDBC developers, new features include: JDBC 4.0, Advanced security in JDBC-Thin (Kerberos, Radius), Database Change Notification, better RAC support; performance, diagnosability and manageability.

For Java in the Database a new JIT compiler automatically compiles Java code, new ease-of-use features including a JDK-Like interface, database resident jars, output redirect, property management interface, and utilities.

For .NET developers: new data access features with Oracle Database 11g, including abstract/user-defined data types, ASP.NET providers, and client-side HA features for Oracle grids; new enhancements to the Visual Studio tools: full integration into Visual Studio 2005, source control, and database script management.

For PHP, a new database resident connection pool allows multiple Oracle clients to share a server-side pool of connections, providing application scalability.

OCI improves database scalability and application performance with client query result cache.

Development - Connectivity (e.g. JDBC, ODBC, Wireless) ,Performance Tuning, Security, Design, Programming (e.g. Java/J2EE, JSP, Beans, XML, .NET, BPEL, ADF, AJAX, SQL, PL/SQL, OC4J), 11g Upcoming Features.

Exposure to JDBC, Oracle JDBC, Java in the Oracle database, .NET of the Oracle Database, PHP, and OCI.

Support for new Java/JDBC standards, security and performance enhancements.

New .NET data access features: UDT, ASP.NET providers, client-side HA for Oracle Grids, .NET tools.

Database resident connection pool for PHP and OCI, query result set cache across sessions/threads.

By - Ramesh

TIPS AND TRICKS

Using Enhanced For-Loops with Your Classes

The enhanced for-loop is a popular feature introduced with the Java SE platform in version 5.0. Its simple structure allows one to simplify code by presenting for-loops that visit each element of an array/collection without explicitly expressing how one goes from element to element.
Because the old style of coding didn't become invalid with the new for-loop syntax, you don't have to use an enhanced for-loop when visiting each element of an array/collection. However, with the new style, one's code would typically change from something like the following:

for (int i=0; i<=array.length; i++) {
System.out.println("Element: " + array[i]);
}
To the newer form:

for (String element : array) {
System.out.println("Element: " + element);
}

Assuming "array" is defined to be an array of String objects, each element is assigned to the element variable as it loops through the array.

These basics of the enhanced for-loop were covered in an earlier Tech Tip: The Enhanced For Loop

By - Ramesh

CASE STUDIES

ABOUT STRING POOL

Creating a String

There are two ways to create a String object in Java:

Using the new operator. For example,
String str = new String("Hello");.
Using a string literal or constant expression). For example,
String str="Hello"; (string literal) or
String str="Hel" + "lo"; (string constant expression). – In lining the statement at compile time.
What is difference between these String's creations? In Java, the equals method can be considered to perform a deep comparison of the value of an object, whereas the == operator performs a shallow comparison. The equals method compares the content of two objects rather than two objects' references. The == operator with reference types (i.e., Objects) evaluates as true if the references are identical - point to the same object. With value types (i.e., primitives) it evaluates as true if the value is identical.
The equals method is to return true if two objects have identical content - however, the equals method in the java.lang.Object class - the default equals method if a class does not override it - returns true only if both references point to the same object.
Let's use the following example to see what difference between these creations of string:
public class StringCreationExample {

public static void main (String args[]) {
String str1 = "Hello";
String str2 = "Hello";
System.out.println("str1 and str2 are created by using string literal.");
System.out.println(" str1 == str2 is " + (str1 == str2));
System.out.println(" str1.equals(str2) is " + str1.equals(str2));


String str3 = new String("Hello");
String str4 = new String("Hello");
System.out.println("str3 and str4 are created by using new operator.");
System.out.println(" str3 == str4 is " + (str3 == str4));
System.out.println(" str3.equals(str4) is " + str3.equals(str4));

String str5 = "Hel"+ "lo";
String str6 = "He" + "llo";
System.out.println("str5 and str6 are created by using string
constant expression.");
System.out.println(" str5 == str6 is " + (str5 == str6));
System.out.println(" str5.equals(str6) is " + str5.equals(str6));

String s = "lo";
String str7 = "Hel"+ s;
String str8 = "He" + "llo";
System.out.println("str7 is computed at runtime.");
System.out.println("str8 is created by using string constant
expression.");
System.out.println(" str7 == str8 is " + (str7 == str8));
System.out.println(" str7.equals(str8) is " + str7.equals(str8));

}
}

The output result is:
str1 and str2 are created by using string literal.
str1 == str2 is true
str1.equals(str2) is true
str3 and str4 are created by using new operator.
str3 == str4 is false
str3.equals(str4) is true
str5 and str6 are created by using string constant expression.
str5 == str6 is true
str5.equals(str6) is true
str7 is computed at runtime.
str8 is created by using string constant expression.
str7 == str8 is false
str7.equals(str8) is true

The creation of two strings with the same sequence of letters without the use of the new keyword will create pointers to the same String in the Java String literal pool. The String literal pool is a way Java conserves resources.
String Literal Pool

String allocation, like all object allocation, proves costly in both time and memory. The JVM performs some trickery while instantiating string literals to increase performance and decrease memory overhead. To cut down the number of String objects created in the JVM, the String class keeps a pool of strings. Each time your code creates a string literal, the JVM checks the string literal pool first. If the string already exists in the pool, a reference to the pooled instance returns, it uses Flyweight design pattern. If the string does not exist in the pool, a new String object instantiates then is placed in the pool. Java can make this optimization since strings are immutable and can be shared without fear of data corruption.
For example:

public class Program
{
public static void main(String[] args)
{
String str1 = "Hello";
String str2 = "Hello";
System.out.printlntr1 == str2);
}
}

The result is:
true
Unfortunately, when you use
String a=new String("Hello");
a String object is created out of the String literal pool, even if an equal string already exists in the pool. Considering all that, avoid new String unless you specifically know that you need it!
For example:
public class Program
{
public static void main(String[] args)
{
String str1 = "Hello";
String str2 = new String("Hello");
System.out.print(str1 == str2 + " ");
System.out.print(str1.equals(str2));
}
}

The result is:
false true
A JVM has a string pool where it keeps at most one object of any String. String literals always refer to an object in the string pool. String objects created with the new operator do not refer to objects in the string pool but can be made to using String's intern() method. The java.lang.String.intern() returns an interned String, that is, one that has an entry in the global String pool. If the String is not already in the global String pool, then it will be added.
For example:

public class Program
{
public static void main(String[] args)
{
// Create three strings in three different ways.
String s1 = "Hello";
String s2 = new StringBuffer("He").append("llo").toString();
String s3 = s2.intern();

// Determine which strings are equivalent using the ==
// operator
System.out.println("s1 == s2? " + (s1 == s2));
System.out.println("s1 == s3? " + (s1 == s3));
}
}

The output is:
s1 == s2? false
s1 == s3? True
There is a table always maintaining a single reference to each unique String object in the global string literal pool ever created by an instance of the runtime in order to optimize space. That means that they always have a reference to String objects in string literal pool, therefore, the string objects in the string literal pool not eligible for garbage collection.
String Literals in the Java Language Specification Third Edition
Each string literal is a reference to an instance of class String. String objects have a constant value. It is not editable.
Thus, the test program consisting of the compilation unit:
package testPackage;
class StringTest {
public static void main(String[] args) {
String hello = "Hello", lo = "lo";
System.out.print((hello == "Hello") + " ");
System.out.print((Other.hello == hello) + " ");
System.out.print((other.Other.hello == hello) + " ");
System.out.print((hello == ("Hel"+"lo")) + " ");
System.out.print((hello == ("Hel"+lo)) + " ");
System.out.println(hello == ("Hel"+lo).intern());
}
}


class Other { static String hello = "Hello"; }
and the compilation unit:
package other;
public class Other { static String hello = "Hello"; }

Produces the output:
true true true true false true
This example illustrates six points:
Literal strings within the same class in the same package represent references to the same String object.
Literal strings within different classes in the same package represent references to the same String object.
Literal strings within different classes in different packages likewise represent references to the same String object.
Strings computed by constant expressions are computed at compile time and then treated as if they were literals.
Strings computed by concatenation at run time are newly created and therefore distinct.
The result of explicitly interning a computed string is the same string as any pre-existing literal string with the same contents.

By - Senthil