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
}
}
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.add("Bronze");
navSet.add("Silver");
navSet.add("Gold");
navSet.add("Platinum");
navSet.add("Diamond");
String item = null;
for (Iterator
item = it.next();
System.out.println("Navigable items in aescending order-->" + item);
}
System.out.println("------------------------------------------------");
for (Iterator
item = it.next();
System.out.println("Navigable items in descending order-->" + item);
}
Set
System.out.println("------------------------------------------------");
for (Iterator
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
item = it.next();
System.out.println("Navigable items in after removing the first item-->"+ item);
}
}
}
3)
4) New methods in 'Class'
<< todo>>
<< todo>>
2 comments:
Hi please cover all features of java 1.6. but you covered here only few features, it is not enough
Thanks for your valuable information of java 1.6 features ...but it needs to cover all features of 1.6 with programs
Post a Comment