What are the the new features in J2SE 5 or java 1.5 ?
- Java 5.0 New Features
- Autoboxing/Unboxing
- Enumerations
- Varargs
- Enhanced for loop
- Metadata-Annotations
- Java Generics
Java 5.0 New Features |
Java 5.0 New features are listed in short here
- Autoboxing/Unboxing
- Enumerations – with ‘enum’ keyword
- Varargs – The last parameter of a method can now be declared using a type name followed by three dots.
- Enhanced ‘for’ loop
- Metadata-Annotations – allows language constructs such as classes and methods to be tagged with additional data, which can then be processed by metadata-aware utilities.
- Generics – Provides compile-time (static) type safety for collections and eliminates the need for most typecasts (type conversion).
Boxing Vs AutoBoxing with Wrapper Classes |
- Java Uses primitive types such as byte,short,int,long,float,double,char and boolean, to hold the basic data type supported by the language.
- And thus,the primitive types are not part of the Object hierarchy, and they do not inherit Object.
- primitive types are used for the sake of performance. Despite the performance benefit, there are times when you will need an object representation.
Eg. You can’t pass a primitive type by reference to a method. though many of the standard data structures implemented by java operate on objects. - So, Java provides type wrappers, which are classes that encapsulates a primitive type within an Object.
- Type wrappers are Byte, Short,Integer,Long, Float, Double, Character,Boolean. that provide wide ranage of methods that allow you to fully integrate the primitive types into java object hierarchy.
Methods available:
byte byteValue()
short shortValue()
int intValue()
long longValue()
float floatValue()
double doubleValue()
char charValue()
boolean booleanValue()
Demonstrate a type wrapper.
class Wrap {
public static void main(String args[]) {
Integer iOb = new Integer(100);
int i = iOb.intValue();
System.out.println(i + " " + iOb); // displays 100 100
}
}
Boxing -> The process of encapsulating a value within an object is called Boxing.
Integer iOb = new Integer(100);
Unboxing -> The process of extracting a value from a type wrapper is called UnBoxing.
int i = iOb.intValue();
Demonstrate a autoboxing/unboxing.
class AutoBox {
public static void main(String args[]) {
Integer iOb = 100; // autobox an int
int i = iOb; // auto-unbox
System.out.println(i + " " + iOb); // displays 100 100
}
}
Autoboxing is the process by which a primitive type is automatically encapsulated(boxed) into its equivalent type wrapper whenever an object of that type is needed. even no need to explicitly construct an object.
Integer iOb = 100; // autobox an int
Auto-unboxing is the process by which the value of a boxed object is automatically extracted(unboxed) from a type wrapper when its value is needed. There is no need to call method such as intValue(), or doubleValue().
int i = iOb; // auto-unbox
Enumerations |
- Version prior to JDK 5 lacked ono feature that many programmers felt was needed: Enumeration.
- An enumeration is a list of named constants.
- In language such as c++, enumerations are simply lists of named integer constants.
- In Java, an enumeration defines a class type. By example, in java, an enumerations into classes, the concept of the enumeration is greatly expanded.
For example, in java, an enumeration can have constructors, methods, and instance variables.
// An enumeration of apple varieties.
enum Apple {
Jonathan, GoldenDel, RedDel, Winesap, Cortland
}
Apple ap;
ap=Apple.RedDel;
ap=Apple.GoldenDel;
Varargs |
Varargs : Variable- Length Arguments:
- Java has included a feature that simplifies the creation of methods that need to take a variable number of arguments.
- This feature is called varargs and it is short for variable-length arguments.
- A methods that takes a variable number of arguments is called a variable-arity method, or simply a varargs method.
- A variable-length argument is specified by three periods(…) for example
static void vaTest(int ... v)
{
//stmts
}
Demonstrate variable-length arguments
class VarArgs {
// vaTest() now uses a vararg.
static void vaTest(int ... v)
{
System.out.print("Number of args: " + v.length + " Contents: ");
for(int x : v)
System.out.print(x + " ");
System.out.println();
}
public static void main(String args[])
{
// Notice how vaTest() can be called with a
// variable number of arguments.
vaTest(10); // 1 arg
vaTest(1, 2, 3); // 3 args
vaTest(); // no args
}
}
Enhanced for loop |
- The For-Each Version of the for Loop :
- It is a new and second form of for loop.
- A for-each stype loop is designed to cycle through a collection of objects, such as an array, in strictly sequential fashion, from start to finish.
Use a for-each style for loop.
class MyForEach {
public static void main(String args[]) {
int nums[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int sum = 0;
// use for-each style for to display and sum the values
for(int x : nums) {
System.out.println("Value is: " + x);
sum += x;
}
System.out.println("Summation: " + sum);
}
}
The for-each loop is essentially read-only.
class WithNoChange {
public static void main(String args[]) {
int nums[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
for(int x : nums) {
System.out.print(x + " ");
x = x * 10; // no effect on nums
}
System.out.println();
for(int x : nums)
System.out.print(x + " ");
System.out.println();
}
}
Use for-each style for on a two-dimensional array.
class MyForEach3 {
public static void main(String args[]) {
int sum = 0;
int nums[][] = new int[3][5];
// give nums some values
for(int i = 0; i < 3; i++)
for(int j=0; j < 5; j++)
nums[i][j] = (i+1)*(j+1);
// use for-each for to display and sum the values
for(int x[] : nums) {
for(int y : x) {
System.out.println("Value is: " + y);
sum += y;
}
}
System.out.println("Sum is : " + sum);
}
}
Metadata-Annotations |
Annotation (Metadata) :
- A new facility was added to java that enables you to embed supplemental information into a source file. This information is called an annotation, does not change the actions of program.
- However, this information can be used by various tools during both development and deployment.
- The term metadata is also used to refer to this feature, but the term annotation is the most descriptive and more commonly used.
eg.
// An annotation type declaration.
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnno {
String str();
int val();
}
- Here Methods act much like fields during the implementation.
- An Annotation can not include an extends clause. Since all annotation types automatically extends the Annotation interface.
- Thus, Annotation is a super-interface of all annotations.
- It is declare within the java.lang.annotation package.
- It overrides hashCode(),equals(),toString() from Object class.
Some Restriction :
- No annotation can inherit another.
- All methods declared by an annotation must be without parameters.
- must return one of the following.
- a primitive type such as int, double
- an object of type String or Class
- an enum type
- Another annotation type
- an array of type
- an annotation cannot be generic.
Click here to explore more details on Java Annotation
Java Generics |
- J2SE 5.0 provides compile-time type safety with the Java Collections framework through generics
- Generics allows you to specify, at compile-time, the types of objects you want to store in a Collection. Then when you add and get items from the list, the list already knows what types of objects are supposed to be acted on.
- So you don’t need to cast anything. The “<>” characters are used to designate what type is to be stored. If the wrong type of data is provided, a compile-time exception is thrown.
Click here to explore more details on Java Generics
Recent Comments