Saturday, December 27, 2014

Java 6 New Features

Java SE 6 incorporates a broad range of enhancements to the infrastructure of Java rather than specific syntax enhancements (unlike Java 5)

• Java SE 6 features include:
– XML and Web services support
– JDBC 4.0 support
– More Annotation types
– More flexible annotation processing
– Jave compiler APIs accessible from programs
– Application client GUI enhancements for both AWT and Swing

• Javas SE 6 address the growth of Web services and XML processing in the Java community
including support for:
– Web Services client stack
– Streaming API for XML (StAX)
– Java Architecture for XML Binding (JAXB) 2.0
– Java API for XML-based Web services (JAXWS) 2.0 Web services metadata
– XML digital signature API

• Java SE 6 includes JDBC 4.0; designed to improve ease of JDBC development by:
– Simplified access to relational data sources with utility classes
– Use of generics and annotations
– Addition of JDBC 4.0 wrapper pattern
– Safe access to vendor-specific APIs
– Automatic driver discovery
– Enhanced connection management
– New data types (including XML and SQL ROWID)


• Annotations were in Java 5.0 allowing developers to embed metadata in Java source code

• Java SE 6 includes additional built-in annotation types and annotation-processing APIs including:
– Web services metadata for the Java Platform (JSR 181)
– Common Annotations for the Java Platform (JSR 250)
– Pluggable Annotation Processing API (JSR 269)

• Java command-line compilers receive input from the file system and report errors using a stream

• Java SE 6 allows the compiler to receive input and/or send output to an abstraction of the file system

• Java programs may now specify compiler directives and process compiler output (this feature was add mostly due to software vendor
requests)

• Java SE 6 enhances application GUI capabilities with changes to both AWT and Swing

• AWT
– Faster splash screens (using native code)
– System tray support (icons & messages)
– Access to browsers and other desktop application “helpers”

• Swing
– Improved drag-and-drop support
– Enhanced layout customization
– Simplified multi-thread programming
– Writing of GIF images

• Changes to Java class file specification (JSR 202)

• Framework to connect Java programs to scripting-language interpreters (JSR 223)

• New bi-directional (allowing backward navigation) collection classes

Java 5 New Features

Java 2 Platform Standard Edition 5.0 (J2SE 5.0 or Java 5 or JDK 1.5) came up with a full bunch of significant new features which are very useful for java developers as well as for JVM in order to improve performance. In this tutorial, you can find some important J2SE 5.0 new features added in JDK 1.5 which are part of day-to-day java coding.

List of most commonly used Java 5 new features

  1. Generics for Collections
  2. Enhanced for Loop (for-each loop)
  3. Autoboxing/Unboxing
  4. Typesafe Enums
  5. Varargs/Vargs (Variable-length Argument Lists)
  6. Static Import
  7. Metadata (Annotations)
  8. Formatting
  9. Scanner

1. Generics

Generics is one of the most useful features added in Java 5. It adds compile-time type safety to the Collections Framework and eliminates the necessity for type casting.
Example to show the benefits of Generics:

    import java.util.*;
    public class GenericsTest 
    {
public static void main(String[] args) 
{
   List<String> names = new ArrayList<String>();
   names.add("Ram");
   names.add("Peter");
   names.add("Khan");
   names.add("Singh");
   names.add(new Date()); // Compiler error
   for(int i = 0; i < names.size(); i++) {
// No need of type casting (String)
String name = names.get(i);
System.out.println(" Name = " + name);
   }
}
    }

The above example program is written using Generics feature of Java 5. Here we are telling java compiler to accept only ‘String’ type objects for the List by using <String> thus providing compile-time type safety. Since we have declared ‘names’ as List of Strings, we need not do type casting for retrieving the elements from ‘names’. If we try to add any object other than String, java compiler shows compiler error and won’t allow us to go-ahead until we remove that black sheep object. So, in order to compile the above program you need to comment/remove the line which adds Date object. This avoids unwanted exceptions during runtime when we try to cast/use the objects stored under List. If you wanna see what does this mean, run the following code:
    List names = new ArrayList();
    names.add("Ram");
    names.add("Peter");
    names.add("Khan");
    names.add("Singh");
    names.add(new Date()); // Compiler accepts
    for(int i = 0; i < names.size(); i++) {
        // Gets Exception for Date object
        String name = (String)names.get(i);
        System.out.println(" Name = " + name);
    }
In this old Java code, compiler accepts Date() object to be added to names List without any issue. But in runtime while retrieving that Date object, you will get the following exception:
    Exception in thread "main" java.lang.ClassCastException: 
        java.util.Date cannot be cast to java.lang.String
So, to avoid such kind of unwanted exceptions, we can use Generics to provide compile-time type safety to collections.
We can also nest generics as follows:
    Map<Integer, List<String>> mapStudents = new HashMap<Integer, List<String>>();

Here is a small excerpt from the definitions of the interfaces List and Iterator in package java.util:
public interface List <E> {
    void add(E x);
    Iterator<E> iterator();
}
public interface Iterator<E> {
    E next();
    boolean hasNext();
}

You might imagine that List<Integer> stands for a version of List where E has been uniformly replaced by Integer:
public interface IntegerList {
    void add(Integer x);
    Iterator<Integer> iterator();
}

A note on naming conventions. We recommend that you use pithy (single character if possible) yet evocative names for formal type parameters. It's best to avoid lower case characters in those names, making it easy to distinguish formal type parameters from ordinary classes and interfaces. Many container types use E, for element, as in the examples above. We'll see some additional conventions in later examples.

Generic References: Oracle.

Six important points about Java 5 Generics:
  1. Use of wildcards with extends or super to increase API flexibility
  2. Generics are implemented using Type Erasure
  3. Generics does not support sub-typing
  4. You can't create Generic Arrays
  5. Use of Multiple Bounds
  6. Generic Method (when to use Generic method vs Wildcard)

1.1 Java Generic's wildcards

There are times that you need to work not only with T but also with sub types of T. For example, the addAll method in the Collection interface which adds all the elements in the specified collection to the collection on which it is called. addAll method has the signature
boolean addAll(Collection<? extends E> c)

This ? extends E makes sure that you can not only add collection of type E but also of subtype of E. ? is called the wildcard and ? is said to be bounded by E. So,if we create a List of Number then we can not only add List of number but we can also add List of Integer or any other subtype of Number.
List<Number> numbers = new ArrayList<Number>();
ArrayList<Integer> integers = new ArrayList<Integer>();
ArrayList<Long> longs = new ArrayList<Long>();
ArrayList<Float> floats = new ArrayList<Float>();
numbers.addAll(integers);
numbers.addAll(longs);
numbers.addAll(floats);

So far we have covered the use of extends with wildcards and we saw that API became more flexible after using extends . But where will we use super? Collections class has a method called addAll which add all the specified elements to the specified collection. It has following signature
public static <T> boolean addAll(Collection<? super T> c, T... elements) ;

In this method you are adding elements of type T to the collection c. super is used instead of extends because elements are added into the collection c whereas in the previous example of Collection interface addAll method elements were read from the collection.

PECS stands for producer extends, consumer super. It proves very helpful whenever you are confused about whether you should use extends or super.

It is a mechanism in Java Generics aimed at making it possible to cast a collection of a certain class, e.g A, to a collection of a subclass or superclass of A. This text explains how.
Here is a list of the topics covered:

The Basic Generic Collection Assignment Problem: Imagine you have the following class hierarchy:
public class A { }
public class B extends A { }
public class C extends A { }
The classes B and C both inherit from A.

Then look at these two List variables:
List<A> listA = new ArrayList<A>();
List<B> listB = new ArrayList<B>();

Can you set listA to point to listB ? or set listB to point to listA? In other words, are these assignments valid:
listA = listB;
listB = listA;
The answer is no in both cases. Here is why: In listA you can insert objects that are either instances of A, or subclasses of A (B and C). If you could do this:
List<B> listB = listA;
Then you could risk that listA contains non-B objects. When you then try to take objects out of listB you could risk to get non-B objects out (e.g. an A or a C). That breaks the contract of the listB variable declaration.
Assigning listB to listA also poses a problem. This assignment, more specifically:
listA = listB;

If you could make this assignment, it would be possible to insert A and C instances into the List<B> pointed to by listB. You could do that via the listA reference, which is declared to be of List<A>. Thus you could insert non-B objects into a list declared to hold B (or B subclass) instances.

When are Such Assignments Needed?
The need for making assignments of the type shown earlier in this text arises when creating reusable methods that operate on collections of a specific type.
Imagine you have a method that processes the elements of a List, e.g. print out all elements in the List. Here is how such a method could look:
public void processElements(List<A> elements)
{
   for(A o : elements)
   {
      System.out.println(o.getValue());
   }
}
This method iterates a list of A instances, and calls the getValue() method (imagine that the class A has a method called getValue()).
As we have already seen earlier in this text, you can not call this method with a List<B> or a List<C> typed variable as parameter.

Generic Wildcards: The generic wildcard operator is a solution to the problem explained above. The generic wildcards target two primary needs:
  • Reading from a generic collection
  • Inserting into a generic collection
There are three ways to define a collection (variable) using generic wildcards. These are:
  • List<?>                        listUknown = new ArrayList<A>();
  • List<? extends A>       listUknown = new ArrayList<A>();
  • List<? super   A>        listUknown = new ArrayList<A>();
The following sections explain what these wildcards mean.

The Unknown Wildcard "?"
List<?> means a list typed to an unknown type. This could be a List<A>, a List<B>, a List<String> etc. Since the you do not know what type the List is typed to, you can only read from the collection, and you can only treat the objects read as being Object instances. Here is an example:
public void processElements(List<?> elements)
{
   for(Object o : elements)
   {
      System.out.println(o);
   }
}
The processElements() method can now be called with any generic List as parameter. For instance a List<A>, a List<B>, List<C>, a List<String> etc. Here is a valid example:
List<A> listA = new ArrayList<A>();
processElements(listA);

The extends Wildcard Boundary
List<? extends A> means a List of objects that are instances of the class A, or subclasses of A (e.g. B and C).

When you know that the instances in the collection are of instances of A or subclasses of A, it is safe to read the instances of the collection and cast them to A instances. Here is an example:

public void processElements(List<? extends A> elements)
{
   for(A a : elements)
   {
      System.out.println(a.getValue());
   }
}
You can now call the processElements() method with either a List<A>, List<B> or List<C>. Hence, all of these examples are valid:

List<A> listA = new ArrayList<A>();
processElements(listA);

List<B> listB = new ArrayList<B>();
processElements(listB);

List<C> listC = new ArrayList<C>();
processElements(listC);
The processElements() method still cannot insert elements into the list, because you don't know if the list passed as parameter is typed to the class A, B or C.

The super Wildcard Boundary

List<? super A> means that the list is typed to either the A class, or a superclass of A.

When you know that the list is typed to either A, or a superclass of A, it is safe to insert instances of A or subclasses of A (e.g. B or C) into the list. Here is an example:
public static void insertElements(List<? super A> list){
    list.add(new A());
    list.add(new B());
    list.add(new C());
}
All of the elements inserted here are either A instances, or instances of A's superclass. Since both B and C extend A, if A had a superclass, B and C would also be instances of that superclass.

You can now call insertElements() with either a List<A>, or a List typed to a superclass of A. Thus, this example is now valid:
List<A>      listA      = new ArrayList<A>();
insertElements(listA);

List<Object> listObject = new ArrayList<Object>();
insertElements(listObject);

The insertElements() method cannot read from the list though, except if it casts the read objects to Object. The elements already present in the list when insertElements() is called could be of any type that is either an A or superclass of A, but it is not possible to know exactly which class it is. 

However, since any class eventually subclass Object you can read objects from the list if you cast them to Object. Thus, this is valid:
Object object = list.get(0);

But this is not valid:
A object = list.get(0);

1.2 Generics are implemented using Type Erasure

In Java a class or an interface can be declared to define one or more type parameters and those type parameters should be provided at the time of object construction. For example,
List<Long> list = new ArrayList<Long>();
list.add(Long.valueOf(1));
list.add(Long.valueOf(2));
In the example shown above a List is created which can only contain elements of type Long and if you try to add any other type of element to this list, it will give you compile time error. It helps detect errors at compile time and makes your code safe. Once this piece of code is compiled ,the type information is erased resulting into similar byte code as we would have got if the same piece of code was written using Java 1.4 and below. This results in binary compatibility between different versions of Java. So, a List or List<> are all represented at run-time by the same type, List.

1.3 Generics does not support sub typing

Generics does not support sub-typing which means that List is not considered to be a sub-type of List, where S is a subtype of T. For example,
List<Number> numbers = new ArrayList<Integer>(); // will not compile

The piece of code shown above will not compile because if it compiles than type safety can't be achieved. To make this more clear, lets take the following piece of code shown below where at line 4 we are assigning a list of long to a list of numbers. This piece of code does not compile because if it could have compiled we could add a double value in a List of longs. This could have resulted in ClassCastException at runtime and type safety could not be achieved.
List<Long> list = new ArrayList<Long>();
list.add(Long.valueOf(1));
list.add(Long.valueOf(2));
List<Number> numbers = list; // this will not compile
numbers.add(Double.valueOf(3.14));

1.4 We can't create Generic Array

You can't create generic arrays as shown below because arrays carry runtime type information about the type of elements . Arrays uses this information at runtime to check the type of the object it contains and will throw ArrayStoreException if the type does not match. But with Generics type information gets erased and the array store check will succeed in cases where it should fail.
T[] arr = new T[10];// this code will not compile

You can't even create Arrays of Generic classes of interfaces. For example, the code shown below does not compile.
List<Integer>[] array = new List<Integer>[10]; // does not compile

Arrays behave differently from the collections because arrays are covariant by default, which means that S[] is a subtype of T[] whenever S is a subtype of T, where as Generics does not support covariance. So, if the above code had compiled then the array store check would succeed in cases where it should fail. For example,
List<Integer>[] ints = new List<Integer>[10]; // does not compile
Object[] objs = ints;
List<Double> doubles = new ArrayList<Double>();
doubles.add(Double.valueOf(12.4));
objs[0] = doubles; // this check should fail but it succeed

If the generic arrays were allowed, then we could assign ints array to an object array because arrays are covariant. After that we could add a List of double to the obj array. We will expect that this will fail with ArrayStoreException because we are assigning List of double to an array of List of integers. But the JVM cannot detect type mismatch because the type information gets erased. Hence the array store check succeeds, although it should have failed.

1.5 Use of multiple Bound

Multiple bounds is one of the generics features which most developer do not know. It allows a type variable or wildcard to have multiple bounds. For example, if you to define constraint such as that the type should be a Number and it should implement Comparable.
public static <T extends Number & Comparable<? super T>> int compareNumbers(T t1, T t2){
   return t1.compareTo(t2);
}

It makes sure that you can only compare two numbers which implement Comparable. Multiple bounds follows the same constraints as followed by the a class i.e. T can't extend two classes ,you have to first specify the class then the interface, and T can extend any number of interfaces.
   public static <T extends String & Number > int compareNumbers(T t1, T t2) // does not work..can't have two classes
   public static <T extends Comparable<? super T> & Number > int compareNumbers(T t1, T t2) // does not work..
   public static <T extends CharSequence & Comparable<T>> int compareNumbers(T t1, T t2)// works..multiple interfaces

1.6 Generic Method

Wildcards are designed to support flexible subtyping, Type argument is being used for polymorphism; its only effect is to allow a variety of actual argument types to be used at different invocation sites.

Generic methods allow type parameters to be used to express dependencies among the types of one or more arguments to a method and/or its return type. If there isn't such a dependency, a generic method should not be used.

It is possible to use both generic methods and wildcards in tandem. Here is the method Collections.copy():
class Collections {
    public static <T> void copy(List<T> dest, List<? extends T> src) {
    ...
}

Now that Class has a type parameter T, you might well ask, what does T stand for? It stands for the type that the Class object is representing. For example, the type of String.class is Class<String>, and the type of Serializable.class is Class<Serializable>. This can be used to improve the type safety of your reflection code.

2. Enhanced For Loop (For-Each Loop)

Enhanced for loop is also referred as ‘forEach’ Loop and is specifically designed to iterate through arrays and collections.

Let’s check the benefits of enhanced for loop with the earlier Generics example program.
import java.util.*;

public class ForEachTest 
{
    public static void main(String[] args) 
    {
        List<String> names = new ArrayList<String>();
        names.add("Ram");
        names.add("Peter");
        names.add("Khan");
        names.add("Singh");

         for (String name : names) {
            System.out.println(" Name = " + name);
        }

    }
}
Here, we can simply iterate through the list ‘names’ and retrieve each element into variable ‘name’. After that, we can do whatever we want with that variable as usual. Enhanced for loop avoids the need for using temporary index variable and simplifies the process of iterating over arrays and collections.

3. Autoboxing / Unboxing

The Autoboxing feature eliminates the need for conversion between primitive data types such as int, float etc. and their respective wrapper classes Integer, Float, etc.

What is Autoboxing in Java?
The implicit automatic conversion of primitive values into corresponding wrapper class objects is known as ‘Autoboxing’.

Autoboxing Examples:

    (1) Integer age = 31;  // Autoboxing: 31 => Integer.valueOf(31) 
The above statement creates a new Integer object with value 31 and assigns to Integer reference variable ‘age’.

    (2) List<Double> weights = new ArrayList<Double>();
         weights.add(64.5);  // Autoboxing: 64.5 => Double.valueOf(64.5) 
         weights.add(73.2);  // Autoboxing: 73.2 => Double.valueOf(73.2) 
Here, two Double objects will be created with values 64.5 & 73.2 and those objects would be added to the list ‘weights’.

What is Unboxing (Auto-unboxing) in Java?
The implicit automatic conversion of wrapper class objects to primitive values is called ‘Unboxing’.

Unboxing Examples:

    (3)  if (age > 25) {  // Unboxing: age => age.intValue()
// do something 
         }
Since age is an Integer object and we can’t perform relational operations (<, >, <=, >=) on objects, first age will be converted to ‘int’ using intValue() method of ‘Integer’ class. Later it will be compared with 25 using ‘>’ relational operator.

    (4)  double totalWeight = weights.get(0) + weights.get(1); // 137.7
         // Unboxing: weights.get(0).doubleValue() 
         //           + weights.get(1).doubleValue()
Like in example (3), we can’t perform arithmetic operations (+, -, *, /, %) on objects, hence the two ‘Double’ objects will be converted to ‘double’ primitive values using doubleValue() method. Now, with no issues, those two double values will be added and the sum would be assigned to ‘totalWeight’.

4. Type Safe Enum:

This new feature allows us to create enumerated types with arbitrary methods and fields. The standard way of representing enumerated types in java using ‘int’ has its own problems. The below example program uses standard enum types:

    public class OldEnumTest
    {
        public static final int SUNDAY = 0;
        public static final int MONDAY = 1;
        public static final int TUESDAY = 2;
        public static final int WEDNESDAY = 3;
        public static final int THURSDAY = 4;
        public static final int FRIDAY = 5;
        public static final int SATURDAY = 6;

        void printAppointment(int day) {
            System.out.println(" Please come on " + day);
        }

        public static void main(String[] args) 
        {
            new OldEnumTest().printAppointment(WEDNESDAY);
        }
    } 

    Output: Please come on 3
Here, there is no type-safety for enum values because compiler can accept any integer number for ‘int’ variable. We can’t stop somebody from passing outside range values to enum variable. For example, we can call printAppointment() method with value 10 or even -25, compiler won’t complain that this is not valid even though we are expecting a number between 0 and 6.

Another issue is that the value displayed would be a number which is not informative. By seeing that number we do not know which enum value it is referring to, moreover we do not even know that it is an enum because of the number. You can understand this better by seeing the above output.

Now consider the following type-safe enum example program:

    public class NewEnumTest 
    {
        public enum Day {
            SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY
        }

        void printAppointment(Day day) {
            System.out.println(" Please come on " + day);
        }

        public static void main(String[] args) 
        {
            new NewEnumTest().printAppointment(Day.TUESDAY);
        }
    } 

    Output: Please come on TUESDAY
Here, compiler accepts only those values which are defined in enum ‘Day’ to be passed to printAppointment() method, thus providing compile time type-safety. Also, the output is so informative that we can understand very easily.

5. Varargs / Vargs (Variable-length argument list)

The Varargs java 5.0 feature avoids the need to group up arguments into an array in order to pass while invoking a method.

Example java code to show the usage of Varargs:
    public class VarargsTest 
    {
        public static void main(String[] args) 
        {
            System.out.println(" Sum = " + sum(41, 22, 58));
        }
        static int sum(int... numbers) {    // varargs
            int sum = 0;
            for (int i = 0; i < numbers.length; i++) {
                sum += numbers[i];
            }
            return sum;
        }
    }

    Output: Sum = 121
In the above example java code, ‘int…’ allows us to pass any number of int arguments to the method ‘sum’. The following examples are all valid.
    sum(98, 33, 105, 10, 7) =>  Sum = 253
    sum(208, 27, 89, 320, 42, 154, 111, 65, 93) =>  Sum = 1109
You can try out float…, char…, String… etc. to understand Varargs much better.

Varargs can also be combined with other arguments, but there should be only one vararg for a method and that vararg should be the last argument for that method.

Java Varargs Examples:
    double calculator(String operation, double... numbers)
    void print(long productId, double price, int qty, String... orderDetails)

6. Static Import:

This feature eliminates the process of qualifying the static members of a class with the class name. Those members can be methods as well as fields.

Example java code to show the usage of Static Import:

    import static java.lang.Integer.*;
    import static java.lang.String.format;

    public class StaticImportTest 
    {
        public static void main(String[] args) 
        {
            int num = parseInt("526");  // => Integer.parseInt()
            Integer num2 = valueOf("123");  // => Integer.valueOf()
            // => String.format()
            System.out.println(format("Numbers: %d, %d", num, num2));
            // => Integer.MAX_VALUE
            System.out.println(" Integer MAX value = " + MAX_VALUE);
        }
    }
Output:
    Numbers: 526, 123
    Integer MAX value = 2147483647

Important Note: Remember the order of keywords. It is ‘import static‘ but not ‘static import’. (I know, it is little bit confused with feature title)

In the above example program, we have imported all static members of the class Integer and format() static method of the class String. Hence, we need not qualify those members like Integer.parseInt(), Integer.valueOf(), String.format(), and Integer.MAX_VALUE. You might have observed that we have used Varargs also in the above example. Yes, String.format() method accepts any number of arguments with syntax “format(String formatStr, Object… args)”.

7. Meta-data (Annotation)

This Java 5 feature lets you avoid writing boilerplate code under many circumstances by enabling tools to generate it from annotations in the source code. This leads to a “declarative” programming style where the programmer says what should be done and tools emit the code to do it. Also it eliminates the need for maintaining “side files” that must be kept up to date with changes in source files. Instead the information can be maintained in the source file.

What are Annotations?
Annotations provide a little extra information about the classes we write. This feature is very useful because we can attach extra information to our code that may determine how it is used.

For example, in J2SE 5.0, we can declare our intent to override a method like toString() in one of our classes as follows:
public class MyClass extends Object {
        @Override
        public String toString() {
            return "My overridden toString() method!";
        }
    }
In the above example, we declared that we are going to override the toString() method using @Override annotation. So the compiler looks in super class (Object) for toString() method with same signature (parameters and return type) and make sure it exists. If, for some reason, we tried to overload toString() by declaring it with different parameters or return type, then the compiler gives an error as there is no such version of toString() method in java.lang.Object. This is really useful to make sure we override correct method and avoid overloading the method by mistake.

We can also define our own Annotations. They basically looks like interfaces, but they can contain values.

Example to explain custom annotations:
    public @interface Meeting 
   {
        String what() default "Project meeting";
        String when();
        String location();
    }
This annotation declares three members: what, when, location and sets them up to have “getters” and “setters” automatically. That means each @Meeting annotation has those three fields associated with it, and we don’t have to define the accessor and mutator methods to set them up. If we define this annotation like this, we can use it to mark code that we use for the XYZ Project Meeting:

    @Meeting(what="Project XYZ",when="11-Apr-2013",location="New York")
      public class MeetingXyz 
     {
        //... class definition
      }
Now the @Meeting type of data is associated with MeetingXyz class. Later on, we could write an analyzer that goes through all of the code and let us know which classes were used at meetings as well as which meetings they were used at and when.

Here, we have discussed about one most commonly used annotation @Override. Few other commonly used annotations are @Deprecated and @SupressWarnings.

8. Formatting

Java 5 provides an interpreter for C language printf-style format strings. This feature provides support for layout justification and alignment, common formats for numeric, string, and date/time data, and locale-specific output. The String class has a method called ‘format’ which takes first argument as formatted string and the remaining arguments as values to be substituted for conversions (%d, %f, etc) inside the formatted string.

The below example shows how to use different type conversions for formatting strings:
    int m1 = 78, m2 = 93, m3= 85;
    int total = m1 + m2 + m3;
    double avg = total / 3.0;
    String result = String.format("Marks: %d, %d, %d. Total: %d, Avg: %.2f", 
                                    m1, m2, m3, total, avg);
    System.out.println(result);

Output:
    Marks: 78, 93, 85. Total: 256, Avg: 85.33
The following are the commonly used conversions in formatting:
%d denotes decimal integer types such as byte, short, long and double.
%f denotes floating point types such as float and double.
%b denotes boolean.
%c denotes character.
%s denotes String.

9. Scanner

The java.util.Scanner class can be used to convert text into primitives or Strings.

Example to show the usage of Scanner:
    java.util.Scanner scanner = new java.util.Scanner(System.in);
    System.out.println("Enter your name:");
    String name = scanner.next();
    System.out.println("Enter your age:");
    int age = scanner.nextInt();

    System.out.printf("Name = %s, age = %d", name, age);

Old Style:
String firstName;
InputStreamReader inStream = new
InputStreamReader(System.in);
BufferedReader inBuf = new BufferedReader(inStream);
System.out.print("Please enter your first name => ");
try {
firstName = inBuf.readLine();
} // end of first try block
catch (IOException e) {
System.out.println("Problem reading first name");
return;
} // end catch block

New Style:
String lastName;
System.out.print("Please enter your last name => ");
Scanner fromkeyboard = new Scanner(System.in);
lastName = fromkeyboard.next();

In the above example, we are reading the user’s name as String and her age as int from console (System.in). We can use Scanner to convert text from console, files, network stream etc into appropriate variables like String, int, float, byte, long, boolean etc.

10. String Builder

• java.lang.StringBuilder classprovides a faster alternative to StringBuffer
– In most ways StringBuilder works exactly thesame as StringBuffer
– StringBuilder is faster than StringBuffer because it is not ThreadSafe (multiple threads should not access StringBuilder objects without Synchronizing)
– Use StringBuilder when speed is important in a single-thread environment and use StringBuffer if multiple threads might require access.
String myString = "How";
StringBuilder myStrBldr = new StringBuilder("How");
myString += " now";
myString += " Brown";
myString += " Cow?";
myStrBldr.append(" now");
myStrBldr.append(" Brown");
myStrBldr.append(" Cow?");
System.out.println("String = " + myString);
System.out.println("StringBuilder = " + myStrBldr);

11. Synchronization

• The java.util.concurrent.locks, java.util.concurrent, and java.util.concurrent.atomic packages are available providing better locking support than provided by the "synchronized" modifier.
• All existing code still works as before
• The java.util.concurrent.xxx packages include interfaces and classes used to simplify synchronization and locking
• The java.util.concurrent.locks.Lock interface has several methods including:
– lock() to obtain a lock (blocks if can’t get lock)
– unlock() to release a lock
– lockInterruptibility() gets lock, allows interruptions
– tryLock() attempts to obtain a lock without a wait.
• The java.util.concurrent.locks.ReentrantLock class behaves like using synchronized does today
• The java.util.concurrent.locks.Condition interface allows complex and multiple conditional waits
• The java.util.concurrent.locks.ReadWriteLock interface allows separate read/write locks


Sunday, December 7, 2014

Maven to use default JDK on MAC


Apple JDK(s) Location: $ ls -latr /System/Library/Java/JavaVirtualMachines
Oracle JDK(s) Location: $ ls -latr /Library/Java/JavaVirtualMachines
Apple Versions Location/System/Library/Frameworks/JavaVM.framework/Versions

There are 2 ways to update java version on mac:
1. Following are equivalent for ~/.bash_profile
  * export JAVA_HOME=$(/usr/libexec/java_home -v 1.7)
  * export JAVA_HOME=`/usr/libexec/java_home -v '1.7*'`
2. update the softlink like below on mac:
  * $ java -version
  * $ cd /System/Library/Frameworks/JavaVM.framework/Versions/
  * $ rm CurrentJDK
  * $ ln -s /Library/Java/JavaVirtualMachines/jdk1.7.0_21.jdk/Contents/ CurrentJDK
  * $ java -version

even after updating version by above mechanism maven still pointing to mac-osx 1.6, because it internally use mac java 6:
$ java -version: java version "1.7.0_51"
$ mvn -version: Apache Maven 3.2.1

When i check it '$M2_HOME/bin/mvn' we can see during start-up, maven look for 2 files '/etc/mavenrc' and '~/.mavenrc'.
So my /etc/mavenrc looks like this:
JAVA_HOME=`/usr/libexec/java_home` # pick the latest version
or we can also define java specific version as : JAVA_HOME=`/usr/libexec/java_home -v 1.7`
bash-style: $ export JAVA_HOME=`/usr/libexec/java_home`
csh-style: % setenv JAVA_HOME `/usr/libexec/java_home`

vi ~/.bash_profile
export JAVA_HOME=$(/usr/libexec/java_home -v 1.7)
# export M2_HOME="/Users/adixit/Documents/XYZ/apache-maven-3.2.3" # brew install take care of setting path
# export PATH="$PATH:${HOME}/Documents/XYZ/apache-maven-3.2.3/bin" # brew install take care of setting path

$ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin


sudo ln -nsf /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 
First run /usr/libexec/java_home -V which will output something like the following:
SISs-MacBook-Pro:Commands adixit$ /usr/libexec/java_home -V
Matching Java Virtual Machines (4):
    1.8.0_25, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home
    1.7.0_72, x86_64: "Java SE 7" /Library/Java/JavaVirtualMachines/jdk1.7.0_72.jdk/Contents/Home
    1.6.0_65-b14-466.1, x86_64: "Java SE 6" /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
    1.6.0_65-b14-466.1, i386: "Java SE 6" /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home

Pick the version you want to be the default (1.8.0_25 for arguments sake) then:
export JAVA_HOME=`/usr/libexec/java_home -v 1.8.0_25`

SISs-MacBook-Pro:Commands adixit$ java -version
java version "1.8.0_25"
Java(TM) SE Runtime Environment (build 1.8.0_25-b17)
Java HotSpot(TM) 64-Bit Server VM (build 25.25-b02, mixed mode)
SISs-MacBook-Pro:Commands adixit$ 

Just add the export JAVA_HOME… line to your shell’s init file.
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
After installing java 7 you will need to do the sudo ln -snf in order to change the link to current java. To do so, open Terminal and issue the command:

$ sudo ln -nsf /Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK

to set JAVA_HOME:
export JAVA_HOME="/Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home"
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 

We can also use jenv script to automatically do execute above task using script.
/System/Library/Frameworks/JavaVM.framework/Versions
/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home

AppleJava6: /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
OracleJava7: /Library/Java/JavaVirtualMachines/jdk1.7.0_72.jdk/Contents/Home
OracleJava8: /Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home


MacBook-Pro:~ adixit$ jenv add /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
oracle64-1.6.0.65 added
1.6.0.65 added
1.6 added

MacBook-Pro:~ adixit$ jenv add /Library/Java/JavaVirtualMachines/jdk1.7.0_72.jdk/Contents/Home
oracle64-1.7.0.72 added
1.7.0.72 added
1.7 added

MacBook-Pro:~ adixit$ jenv add /Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home
oracle64-1.8.0.25 added
1.8.0.25 added

1.8 added

MacBook-Pro:~ adixit$ jenv global 1.7.0.72
MacBook-Pro:~ adixit$ jenv local 1.7.0.72
MacBook-Pro:~ adixit$ jenv shell 1.7.0.72

MacBook-Pro:~ adixit$ jenv versions

References:
  1. Apple way to became root on mac appledoc & terminal.
  2. Give sudo access on macbook.
  3. Change java version on mac and source at github.
  4. jenv utility that let you change environment on mac.
  5. oracle blog
  6. jenv

Installing / Uninstalling IntelliJ & important shortcuts

Uninstalling Intellij on a Mac for the most part is no different from uninstalling any application on a Mac you’ll need to go to the Applications directory and and remove the application bundle though there will be some residual files that will need deleting. 

All application installed in directory "/Application", same of intelliJ as "/Application/IntelliJIdeaXX" where XX stands for version for us XX is 14 ("/Application/IntelliJIdea14").

XX : 14
Follow these steps for a complete removal
  1. Goto /Application/<ApplicationName>  directory right click or drag and move to trash, for some reason if we are not able to delete then run command from terminal window [rm -rf /Application/IntelliJIdea14 as user / or as super user ( sudo) ].
  2. Goto the following directory where ‘username’ is your username and AppName will be something like ‘jetbrains.intellij’ /Users/username/Library/Preferences/AppName
  3. remove all residue files [~ is user home directory /Users/username]:
    1. ~/Library/Caches/IntelliJIdeaXX
    2. ~/Library/Logs/IntelliJIdeaXX
    3. ~/Library/Preferences/IntelliJIdeaXX
    4. ~/Library/Application Support/IntelliJIdeaXX
    5. ~/Library/Preferences/com.appname.plist
      1. ~/Library/Preferences/com.jetbrains.intellij.plist
    6. ~/Library/Preferences/com.jetbrains.intellij.plist.lockfile
    7. ~/Library/Caches/com.jetbrains.intellij
    8. ~/Library/Saved Application State/com.jetbrains.intellij.savedState
    9. /Library/Preferences/AppName
    10. /Library/Preferences/com.appname.plist
    11. /Library/Application Support/AppName
    12. Also clear any files from location ~/.intellij  folder or something similar in your home directory.
References:
1. https://intellij-support.jetbrains.com/entries/23358108
2. https://devnet.jetbrains.com/docs/DOC-181

 Installing Intelli J to use JDK 7 /JDK 8

Open Info.plist in an editor and make following changes and save and exit.
$ vi /Applications/IntelliJ IDEA 14.app/Contents/Info.plist
      <key>JVMVersion</key>
      <string>1.6*</string>
change to 
      <key>JVMVersion</key>
      <string>1.8*</string>
and make sure we have LANG=en_US.UTF-8 in our environment.

Intelli J Shortcut
  1. Search everywhere with Double Shift Click.
  2. Open a file name with 'Shift'+ 'Command'+'O'.
  3. Open recent Files with 'Command'+'E'.
  4. Open Navigation Bar with 'Command'+'up arrow'.
  5. Drag and drop files here from Finder.
  6. Search for a java method in a java class: 'Fn'+'Command'+'F12'.
  7. Control+space: completion (shift: smart)
  8. Command+O to navigate to any class
  9. Option+F7 : Edit /find/find usage
  10. F1: quick documentation
  11. Command+B: navigate declaration
  12. Command+F12: Navigate File structure
  13. Shift+F6: refactor / rename
  14. Control+o: code override methods
  15. Control+I: code implements method of interface
  16. Command+N [code generation]
  17. Option +F1: 
  18. F12: moves the focus from editor to last focus tool window
  19. Option+Command+T: Code / surround with
  20. Navigate implementation of abstract method: option+command+B
  21. Option+up arrow: extend selection.
  22. Option+Command+V: simplify complicated statement (refractor / extract / variable)
  23. Comment(s):Command+/  and option+command+/
  24. Browser with documentation: shift+F1
  25. Command+D: duplicates
  26. Command+Delete: Delete
  27. Command+P: paranthesis parameter
  28. Shift+Command+Delete: Naviagate last editted location - history
  29. Shift+Command+F7: Edit find highlighted item in file (Command+G and Shift+Command+G: to naviage)
  30. Control+Shift+Q: view / context info, to see declaration of current method.
  31. Command+E: view recent files
  32. F2/Shift+F2: to jump between highlighted syntax error.
  33. Option+command+up arrow / option+command+down arrow: shortcut to jump between compiler error messages or search option results
  34. Command+J: live code completion template
  35. Control+ uparrow / Control+down arrow: to move between methods
  36. Control+Shift+J: combine two lines
  37. Shift+Command+V: clipboard paste
  38. Control+H: inheritance hierarchy of a selected class.
  39. Option+Command+O: to search a method or a field in a class.
  40. Option+Shift+C: to quickly review your recent changes to the project
  41. Control+tilt: View quick switch schemes
  42. Shift+Command+Enter: to complete a current statement such as if , try catch return etc.
  43. Option + space: view quick definition
  44. Option+Shift+Commnad+C:Edit copy reference
  45. Control+Option+R: access run / debug dropdown on the main toolbar
  46. Option+Command+F7: Edit find show usage
  47. Shift+Command+A: Help / Find action
  48. To quickly find and run an inspection: option+shift+command+I
  49. Option+Enter: to create test
  50. Alt: multiple cursor
  51. Control+K: commit changes
  52. Command+F: search pane Command+R: replace [Path: Shift]

Check Port bind exception:
1. We can try $ netstat -anp tcp | grep 8080
2. If our netstat doesn't support -p, use lsof then try $ lsof -i tcp:8080
3. to free up run $ kill -9 processId.




Friday, September 5, 2014

Google Chrome, Mac OS X and Self-Signed SSL Certificates

Let's say you have a server with a self-signed HTTP SSL certificate. Every time you hit a page, you get a nasty error message. You ignore it once and it's fine for that browsing session. But when you restart, it's back. Unlike Firefox, there's no easy way to say "yes, I know what I'm doing, ignore this." This is an oversight I wish Chromium would correct, but until they do, we have to hack our way around it.

Caveat: these instructions are written for Mac OS X. PC instructions will be slightly different at PCs don't have a keychain, and Google Chrome (unlike Firefox) uses the system keychain.

So here's how to get Google Chrome to play nicely with your self-signed SSL certificate:

  1. In the address bar, click the little lock with the X. This will bring up a small information screen. Click the button that says "Certificate Information." 
  2. Click and drag the image to your desktop. It looks like a little certificate. 
  3. Double-click it. This will bring up the keychain Access Utility. Enter your password to unlock it.
  4. Be sure you add the certificate to the System keychain, not the login keychain. Click "Always Trust," even though this doesn't seem to do anything.
  5. After it has been added, double-click it. You may have to authenticate again.
  6. Expand the "Trust" section.
  7. "When using this certificate," set to "Always Trust".



or from command line execute following command:
sudo security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain site.crt


That's it! Close Keychain Access and restart Chrome, and your self-signed certificate should be recognized now by the browser.

This is one thing I hope Google/Chromium fixes soon as it should not be this difficult. Self-signed SSL certificates are used *a lot *in the business world, and there should be an easier way for someone who knows what they are doing to be able to ignore this error than copying certificates around and manually adding them to the system keychain.

Saturday, July 19, 2014

Apple important Shortcuts & Tricks






  1. Command + Delete to delete a file, Command + Shift + Delete to delete it permanently even from Trash, Command+Shift+Option+Delete : means delete without confirmation dialog even from trash.
  2. Your security preferences allow installation of only apps from the Mac App Store and identified developers: press Control + Click [Permanently Changes: Preferences > 'Security & Privacy’ > click unlock at the bottom > to allow it by default.]
  3. Command +Control + Power Button : Force Mac to restart
  4. Command + Shift + 3: capture screen to a file
  5. Command + Shift + Control + 3: capture screen to clipboard
  6. Command + Shift + 4: Capture a selection of screen to a file, or press the spacebar to capture just a window.
  7. Command + Shift + Control + 4: Capture a selection of screen to clipboard, or press the spacebar to capture the just a window.
  8. Command + Shift + [+]: Zoom out
  9. Command + Shift + [-] : Zoom in
  10. Command + Spacebar: Spotlight
  11. Command + Tab: switch between windows ; Command+Shift+ Tab: navigate in reverse direction ( and when we press Q, it will close respective tab).
  12. Control + Tab: navigate Tabs, Control+Shift+Tab: navigate in reverse direction.
  13. Command+[:]: display spelling and grammar window.
  14. Command+[;]: find mis-spelled word in the document.
  15. Command+[,]: open front application - preference window.
  16. Command + D: bookmark current page
  17. Command+G: find next occurrence of selection, Command + Shift + G: find previous occurrence of selection.
  18. Command + Z: undo, Command + Shift+Z: redo
  19. Command + Option + ESC: Choose force quit an application.
  20. Command + Shift + Option + ESC ( hold for 3 sec) force quit front most application.
  21. Command + 'Left bracket' [ : Previous browser web page
  22. Command + 'Right bracket' ] : Next browser web page
  23. Delete: backward delete, fn+Delete: forward delete.
  24. Command + Shift + Q: Logout, Command+Shift+Option+Q: logout immediately.
  25. Finder Shortcuts:
    1. Select All: Command + A
    2. De-select All: Command + Option +A
    3. Open Application Folder: Command + Shift +A
    4. Duplicate selected Item: Command+D
    5. Open Desktop Folder: Command + Shift +D
    6. Show View option: Command + J
    7. Connect to server: Command +K
    8. Open network window: Command + Shift +K
    9. Make aliases of selected items: Command +L
    10. Minimize window: Command +M
    11. Minimize all window: Command +Option +M
    12. New finder window: Command +N
    13. New Folder: Command +Shift +N
    14. Close curent tab: command + W, re-open closed tabs: command +Z
    15. New browser Window: Command + N
    16. command + Q: close safari window and all tabs.
    17. Go to end of line: Command + Right arrow
    18. Go to beginning of line: Cmd-left arrow
    19. Go to end of all the text: Cmd-down arrow
    20. Go to beginning of all the text:Cmd-up arrow
    21. Go to end of current or next word: Option-right arrow
    22. Go to beginning of current or previous word: Option-left arrow
  26. Finder PathBar and few handy functions (The finder PathBar is disabled by default, but it's very simple to enables it: 'Open Finder Window' > 'Select show PathBar from the view menu' > 'The PathBar will now display in all finder window'):
    1. Double clicking any of the folder in PathBar will take you to that folder.
    2. We can move files and folders to any element in PathBar by simply dragging and dropping them.
      1. We can copy them by holding the option key as we drag them.
      2. or create an alias to an item by holding down the Command + Option keys while we drag them.
  27. Terminal:
    1. Show hidden files on mac:
      1. $ defaults write com.apple.finder AppleShowAllFiles TRUE
      2. $ killall Finder
    2. Disable Lion/Mountain Lion's Pop-up Accent Window:
      1. $ defaults write -g ApplePressANdHoldEnabled -bool false
    3. Change the Default Backup Periods in Time Machine
      1. [This makes Time Machine backup now every 30 mins = 30*60 secs]$sudo defaults write /System/Library/Launch Daemons/com.apple.backupd-auto StartInterval -int 1800
    4. Drag Dashboard Widgets onto the Desktop (like windows 7)
      1. $ defaults write com.apple.dashboard devmode YES
    5. Change the file format of Screenshots (default is PNG): 
      1. $ defaults write com.apple.screencapture type file-extension [replace file-extension with three letter abbreviated word JPEG for JPG, PDF]
    6. Kill the Dashboard all together
      1. $ defaults write com.apple.dashboard mcx-disabled -boolean YES
      2. Then restart the Dock using the command $ killall Dock
    7. Securely erase free space: When we delete files on our mac, OS-X still leaves fragments of the file all over the free space on our hard disk drive, until these are written over by new files. if you want to securely delete all the remaining fragments on a hard disk drive (for eg. if we are going to sell our mac) then execute the following command:
      1. [replace name-of-drive with the drive we want to erase. This command uses a special algorithm to wipe over each free area of space 35 times, far above the US Dept of Defense's standard, which only requires 7 passes. Be aware through that this process can take days on larger drives] $ diskutil secureErase freespace 3 /Volumes/name-of-drive
    8. Remote Login: Being able to control your Mac remotely via SSH, or secure shell, is far more advantageous that screen sharing as it uses less system resources and less bandwidth. The first thing we will have to do is enable Remote Login on our Mac, which we can do by heading over the 'System Preferences' then 'Sharing' then Click on 'Remote Login'. 
      1. $ ssh -l username remote-address [replace username with your username you use to log into OS X and remote-address with the IP-address given to you in the sharing pane. You can now control your Mac and execute Terminal commands remotely, a real plus.
    9. Talking Mac: $ say command [eg. say Hello World !] The words will be spoken in your Mac's default voice but if we want to change this, then simply head over to System Preferences then either Dictation and Speech (on Mountain Lion) or simply Speech (on Lion). Here you can select different voices and download new one from Apple's servers if you fancy. Another "useful" feature is the ability to convert an entire text file into speech, if you so fancy. Simply enter: $ say -o audio.aiff -f FILENAME.txt  [replace FILENAME.txt with your own file. This will create a reading of your file as an AIFF file audio.aiff in Terminal's default directory].
    10. Stop / kill server instance:
      1. Control +z : (sig 3+ signal but it will not clear the instance process id)
      2. Control +c : (Kill and clear instance as well)
    11. Enable root
      1. terminal: $ sudo -s
      2. Enable from Mac GUI
        1. Open user and group preferences, click login options, then click the lock icon to unlock it. If necessary they your password then click unlock.
        2. In the Network Account Server Section, click Join or Edit.
        3. Click open Directory Utility.
        4. Click the lock icon to unlock it, then enter your administrator name and password.
        5. Do one of the following:
          1. Choose Edit > Enable Root User, then enter a root user password in the password and verify fields.
          2. Choose Edit > Disable Root User.
          3. Choose Edit > Change Root Password, then enter a new root user password.
    12. sudo interactive shell
      1. interactive shell: $ sudo -i
      2. exit interactive shell: $ exit +Enter    or   ^D ( Control + D)
  28. MAC OS X Tips:
    1. Shift + Click 'Maximize button' to fill screen.
    2. use $ purge command to force release unused blue memory.
    3. Remove icon from menu bar: hold command key, click icon, drag it off menu bar.
    4. Go to Libary folder ( ~/Library ) or (~/Application).
    5. Finder SideBar, open finder , and click 'Show SideBar', we can create shortcut of our folder for easy access.
    6. Control the apps that Launch at startup, this will reduce mac startup time: 'System & Preferences' > 'User and Groups' > then look for 'Login Items' button ( those are the apps that launch for me).
    7. Lock Macbook: To do this, head to System Preferences > Security & Privacy > General. Check the box next to “Require Password” and set an interval that meets your workflow. If you want the highest level of security, set it to “immediately”, following are 2 ways:
      1. Lock Screen: To lock your Mac’s screen, simultaneously press the following keys: Control + Shift + Eject. If you have a newer Mac that doesn’t have an optical drive(and thus has no eject key on the keyboard, such as the Retina Macbook Pro), the command is Control + Shift + Power.
      2. Sleep the Mac entirely: MacBook owners are familiar with sleep; it occurs every time they shut their computer’s lid, or automatically after a user-defined period of time. But users can also trigger an immediate sleep state with a simple keyboard command: Command + Option + Eject. Optical drive-less Mac owners can repeat the substitution discussed above and replace the Eject key with the Power key, resulting in a command for Retina Macbook Pro owners, et al. of Command + Option + Power.
  29. KeyChain: 
    1. identityserviced wants to use the "Local Items" keychain: delete the keychain entries under following folder " ~/Libarary/Keychains/" and restart the system.