Saturday, January 5, 2013

Firefox 18 Beta Boasts Faster JavaScript


The latest incarnation of Firefox features faster JavaScript and even has Mac users excited.
Firefox 18 beta rings in 2013 with a slew of new features, but what most people are interested in
is the lightning-fast JavaScript.

The just in time (JIT) compiler has been nicknamed IonMonkey, in keeping with the tradition
of monkey monikers such as JaegerMonkey of Firefox past. IonMonkey was introduced in
September 2012, but it’s just beginning to pick up steam.

IonMonkey has the ability to allow for complex JavaScript at a much faster speed than previously
thought possible. Whether you work in workers’ comp or programming for a federal agency,
this might make your daily life easier. There are a few things to consider, but it’s estimated that
IonMonkey can improve performance up to 26 percent. And when your performance is upped
more than one quarter, that’s cause for celebration.

Thursday, January 3, 2013

Spring - How to inject List or Map using bean?

In this example we will see how to How to inject List or Map using Spring bean. For complete setup using eclipse you can refer my previous article, Spring Hello World Example.

About the example - We have a class Country.java with two instance variables city as Map and president as List. We will set those values using Spring bean with the help of Spring configuration file  applicationContext.xml. Please see the self explanatory codes below

Country.java.


Serialization in Java


Serialization is used to save the object and all of its instance variables (except transient) in an external file mostly with .ser extension. With the help of Serialization you can reconstruct the same object latter.

The Serialization is done with the help of below two methods

// serialize objects and write them to a stream
ObjectOutputStream.writeObject() 

// read the stream and deserialize objects
ObjectInputStream.readObject()

About the example - We have a Dog class, which we will be Serializing. Note that Class which to be serialize must implements the Serializable interface. Serializable is a marker interface, it has no methods to implement. In SerializeDog.java we will create a Dog object d1 serialize it and write it to a dog.ser file and latter we will deserialize the object.

Please see the java codes below



Wednesday, January 2, 2013

Java 5 - Using Static Imports


Static Imports concept was introduced with Java 5, the static import statement just provides greater keystroke-reduction capabilities. Static imports can be used when you want to use a class's static members. You can use this feature on classes available in the API and on your own classes.

Rules associated with Static import


  • Correct syntax is import static, you can't say static import.
  • Check for ambiguously named static members. For example if you do a static import for both the Integer class and the Long class, referring to MAX_VALUE will cause a compiler error, since both Integer and Long have a MAX_VALUE constant, and Java won't know which MAX_VALUE you're referring to.
  • You can do a static import on static object references, constants (static and final), and static methods.


For example - 

Before static imports:

public class TestStatic {
public static void main(String[] args) {
System.out.println(Integer.MAX_VALUE);
System.out.println(Integer.toHexString(42));
}
}

After static imports:

import static java.lang.System.out;
import static java.lang.Integer.*;

public class TestStaticImport {
public static void main(String[] args) {
out.println(MAX_VALUE); 
out.println(toHexString(42)); 
}
}

Both of the above classes produces same output.

Java - Source File Declaration Rules


Below are the rules associated with declaring classes, import statements, and package statements in a java source file.


  • There can be one and only one public class per source code file and can have more than one nonpublic class.
  • If there is a public class in a file, the name of the file must match the name of the public class. For example, a class declared as public class HelloWorld { } must be in a source code file named HelloWorld.java. Files with no public classes can have a name that does not match any of the classes in the file.
  • If the class is part of a package, the package statement must be the first line in the source code file, before any import statements that may be present.
  • If there are import statements, they must go between the package statement (if there is one) and the class declaration. If there isn't a package statement, then the import statement(s) must be the first line(s) in the source code file. If there are no package or import statements, the class declaration must be the first line in the source code file.
  • import and package statements apply to all classes within a source code file. In other words, there's no way to declare multiple classes in a file and have them in different packages, or use different imports.
  • Comments can appear at the beginning or end of any line in the source code file, they are independent of any of the rules discussed above.