This blog describes some of the cool features of jdk5.0.
1. Formatted IO
This basically can divide into two parts .This features makes java more similar to the features available in C.
1. Formatting features for the out put.
In this feature printf() method is introduced so that just like in C the formatting can be done depending on the data types.
Eg.
System.out.printf("name count%n");
System.out.printf("%s %5d%n", user,total);
See more about formatters in java.util.Formatter
2. Formatting features for input
This is another feature which simplifies the command line arguments operations in java.In the earlier versions java is not much popular in this arena. There is a Scanner class available in jdk5.0.This Scanner class is used for accepting System.in, different files streams etc and access the values available in the stream specified in side the java programe.
Eg .
Scanner s= new Scanner(System.in);
String param= s.next();
int value=s.nextInt();
s.close();
Static Import
Using this ,no need to import the classes to access the static members.
Eg. Import java.lang.Math.PI;
Or import java.lang.Math.*;
So we can access the static members directly inside the programe.
double r =500*PI;
Enumerations
Jdk5.0 supports enumerations.This is the way to define set of constants in java program. The enum constants defined in the program of the type of the class its defined.This is basically a replacement of final members in java.
Some of the interesting features are
This can be used with switch statement.
public enum Operation
{ PLUS, MINUS, TIMES, DIVIDE; // Do arithmetic op represented by this constant
double eval(double x, double y){
switch(this) {
case PLUS: return x + y;
case MINUS: return x - y;
case TIMES: return x * y;
case DIVIDE: return x / y;
}
throw new AssertionError("Unknown op: " + this); }}
Enum is just like a class whether we can have constructors methods and member variables. But Enum cannot inherit any class.
The method values() will get array of constants defined and valueOf() used to get the constant value.
Continue....
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment