Thursday, April 11, 2013

what is JPDA and how to use that ?



 JavaTM Platform Debugger Architecture

enhancements to JPDA that have been added in Java SE 6.... coming soon

Monday, April 8, 2013

Example on java.util.Queue and its implementation classes

Example on java.util.Queue  and its implementation classes usage.

package com.pramasa;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Iterator;
import java.util.Queue;
import java.util.concurrent.ArrayBlockingQueue;

public class QueueExample {
    public static void main(String[] args) {
     // Java 6 introduced a double ended queue implementation.       
        Deque dq=new ArrayDeque();
        dq.offer("Bronze");
        dq.offer("Silver");
        dq.offer("Gold");
        dq.offer("Platinum");
        dq.offer("Diamond");   
       
        String item=null;
        for(Iterator it=dq.iterator(); it.hasNext();){
            item=it.next();
            System.out.println("item-->"+item);
        }
        System.out.println("----------------------------");
        for(Iterator it=dq.descendingIterator(); it.hasNext();){
            item=it.next();
            System.out.println("item-->"+item);
        }
        System.out.println("----Some of the methods available------------------------");
        dq.addFirst("Iridium");dq.addLast("PinkDiamond");
       
        for(Iterator it=dq.iterator(); it.hasNext();){
            item=it.next();
            System.out.println("item-->"+item);
        }


       //  Java 5 introduced ArrayBlockingQueue 
      // and below code describes how 'add' throws exception when we try to add items more than its   
      // actual size
        Queue q= new ArrayBlockingQueue(5);
        q.offer("Malay");
        q.offer("English");
        q.offer("Chinese");
        q.offer("Tamil");
        q.offer("Hindi");
        q.offer("Telugu"); // Here we don't get any exception
       
        for(Iterator it=q.iterator(); it.hasNext();){
            item=it.next();
            System.out.println("ArrayBlockingQueue item using method Offer-->"+item);
           
        }
        q.add("Malay");
        q.add("English");
        q.add("Chinese");
        q.add("Tamil");
        q.add("Hindi");
        //q.add("Telugu");// Here we get the IllegalStateException
       
        for(Iterator it=q.iterator(); it.hasNext();){
            item=it.next();
            System.out.println("ArrayBlockingQueue item using  method add-->"+item);
        }
    }
}




Saturday, April 6, 2013

Java 7 New Features and Examples

Coming soon...

Java 6 New Features with Example Programs


1) New Class java.io.Console was Introduced in Java6 to read text from Console. It has advantage that it can read passwords from the console without echoing it on the console.

Please follow the below program , to find the how it was in earlier versions and advantage of class Console.


package com.pramasa;

import java.io.BufferedReader;
import java.io.Console;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
/*
 * Please execute the following program from command prompt.
 */
public class Features {

/**
* @param args
*/
public static void main(String[] args) {
// A. Using the Console class methods... from jdk 1.6

Console console = System.console();
System.out.println("Please key in  text and press enter:");
String readLine = console.readLine();
System.out.println("Line from Console class:" + readLine);

System.out.println("Please key in  password and press enter:");

char[] charArray = console.readPassword();
String passwordFromConsole = "";
for (int i = 0; i < charArray.length; i++) {
passwordFromConsole = passwordFromConsole + charArray[i];
}
System.out.println("Password from Console class:" + passwordFromConsole);

// Other approaches used to follow to get text from console
// B. InputStream class
BufferedReader bis = new BufferedReader(
new InputStreamReader(System.in));
String passwordFromInputStream = null;
try {
passwordFromInputStream = bis.readLine();
// There are no special methods to read passwords in InputStream class without echoing
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(passwordFromInputStream);

// C . Scanner class from java.util.Scanner
String scannedLines = "";
Scanner scanner = new Scanner(System.in);
while (scanner.hasNextLine()) {
scannedLines += scanner.nextLine();
}
System.out.println(scannedLines);
// Here both Scanner and InputStreamReader used the class InputStream
// (System.in) as an argument to read from the console and they are
// designed to read from other input sources also like File, Strings...
// Where as Console Class is designed for single purpose to read from
// Console.
// Also No need to handle exception if we use Console
}
}

// Please provide your valuable comments to improve the above program
2)     New Collections introduced NavigableSet and ConcurrentSkipListSet,

package com.pramasa;
import java.util.HashSet;
import java.util.Iterator;
import java.util.NavigableSet;
import java.util.Set;
import java.util.concurrent.ConcurrentSkipListSet;

public class NavigableCollection {

public static void main(String[] args) {

NavigableSet navSet = new ConcurrentSkipListSet();
navSet.add("Bronze");
navSet.add("Silver");
navSet.add("Gold");
navSet.add("Platinum");
navSet.add("Diamond");

String item = null;

for (Iterator it = navSet.iterator(); it.hasNext();) {
item = it.next();
System.out.println("Navigable items in aescending order-->" + item);

}
System.out.println("------------------------------------------------");
for (Iterator it = navSet.descendingIterator(); it.hasNext();) {
item = it.next();
System.out.println("Navigable items in descending order-->" + item);

}
Set hashSet = new HashSet(navSet);
System.out.println("------------------------------------------------");
for (Iterator it = hashSet.iterator(); it.hasNext();) {
item = it.next();
System.out.println("Set items with no specific order-->" + item);
// we can't guarantee the order the with HashSet
// Where as TreeSet implements sortedSet and new interface
// NavigableSet.
// So it provides more methods for the ordering and navigation.
// By Default TreeSet follows natural ordering
}
// Some of the methods introduced in jdk 1.6
System.out.println("Lower item than the Gold in the set-->"+ navSet.lower("Gold"));
// Lower method works by assuming all elements are in ascending order
System.out.println("------------------------------------------------");
String firstItem = navSet.pollFirst();
System.out.println("removed the first item-->" + firstItem);
for (Iterator it = navSet.descendingIterator(); it.hasNext();) {
item = it.next();
System.out.println("Navigable items in after removing the first item-->"+ item);
}
}
}


3)

4) New methods in 'Class'
       << todo>>