There are a few features and enhancements that are added in jdk1.5. These are listed below:
Java Language Features were introduced for the ease of programmers and to make it simpler. They include the following:
This feature of Java was introduced in 2004 and is used providing compile-time type safety,”.
Read on to find out more about it, its uses as well as disadvantages.
When you take an element out of a Collection, you must cast that element to the type of element that is stored in the collection.
Besides being inconvenient, this is unsafe. The compiler does not check if your cast is the same as the collection's type, so it can fail at run time.
Generics provide a way for you to communicate the type of a collection to the compiler so that it can be checked.
Once the compiler knows the element type of the collection, the compiler can crosscheck whether you have used the collection consistently and can then insert the correct casts on values being taken out of the collection.
The code using generics is clearer and safer. We have eliminated an unsafe cast and a number of extra parentheses.
When you see , read as "of type" For example: ArrayList al = new ArrayList () ; Read as : al is type of String.
Example of collection without Generics
package com.javabykiran;
import java.util.ArrayList;
import java.util.Iterator;
public class GenericsLab1 {
public static void main(String[] args) {
ArrayList al= new ArrayList(); // without Generics
al.add("java");
al.add(new Long("88888809416"));
Iterator itr = al.iterator();
while(itr.hasNext()){
String s =" ";
Object o =itr.next();
if(o instanceof String){
s = (String) o ;
System.out.println(s);
}
if(o instanceof Long){
System.out.println((Long)o);
}
}
}
}
Output:
java
88888809416
Example of collection with Generics:
package com.javabykiran; import java.util.ArrayList; import java.util.Iterator; public class GenericsLab2 { public static void main(String[] args) { ArrayList al =new ArrayList (); //with Generics al.add("java"); al.add("8888809416"); Iterator itr = al.iterator(); while(itr.hasNext()){ String p= itr.next(); System.out.println(p); } } }
Output:
java
88888809416
Note:
There are a number of reasons why we would use generics.
The following code snippet without generics requires casting:
List list = new ArrayList(); list.add("hello"); String s = (String) list.get(0); When re-written to use generics, the code does not require casting: List list = new ArrayList (); list.add("hello"); String s = list.get(0); // no cast
java.util.ArrayList.get(int index)
The method returns the element at the specified position in this list.
public void add(int index, E element);
This adds the element to the specified index in the list.
Example:
package com.javabykiran; import java.util.ArrayList; public class ForEachLab3 { public static void main(String[] args) { ArrayList al = new ArrayList (); al.add("www.javabykiran.com"); al.add("8888809416"); for (String string : al) { //for each loop System.out.println(string); } } }
Output:
www.javabykiran.com
8888809416
Features and Enhancements Added In Jdk1.5
The automatic conversion of primitive datatypes into its equivalent wrapper type is called as autoboxing. Its reverse operation is called as Unboxing.
As you know, we can't put an int (or other primitive value) into a collection. Collections can only hold object references, so you have to box primitive values into the appropriate wrapper class (which is an Integer in the case of int). When you take the object out of the collection, you get the Integer that you put in; if you need an int, you must unbox the Integer using the intValue method.
Example of Autoboxing and Unboxing:
package com.javabykiran;
public class BoxingLab1 {
public static void main(String[] args) {
int a = 10;
Integer i = a;
//Boxing-- internally :
Integer j = Integer.valueOf(a);
System.out.println(j);
int c1 = i; //Unboxing -- :
int c2 = i.intValue();
System.out.println(c2);
}
}
An enum is a data type which contains fixed set of constants.
It can be used for days of the week (SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY and SATURDAY), or directions (NORTH, SOUTH, EAST and WEST) etc.
The enum constants are static and final implicitly
It is available from JDK 1.5. It can be thought of as a class, i.e, enum can have constructors, methods and variables.
Enum improves type safety; it can be easily used with a switch statement.
Example
package com.javabykiran;
enum Day {
SUNDAY,MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY }
public class EnumLab1 {
public static void main(String[] args) {
for (Day d : Day.values()) {
System.out.println(d);
}
}
}
Output:
SUNDAY
MONDAY
TUESDAY
WEDNESDAY
THURSDAY
FRIDAY
SATURDAY
Note:
Varargs are Variable Arguments which accepts none or more arguments.
Varargs can be used for methods that can take zero or more arguments.
Before varargs was introduced, we had to overload the methods.
If we are uncertain about the number of arguments we would need, only then should we go for varargs.
Example 1:
package com.javabykiran;
public class VarargsLab {
void m1(String...s){
System.out.println("in a m1 method");
for(String val :s){
System.out.println(val);
}
}
public static void main(String...args) {
//varargs can be apply to main method
VarargsLab v = new VarargsLab();
v.m1(); //0-args
i.m1("www"); //1-args
v.m1("www",".","javabykiran") //3-args
}
}
Output:
in a m1 method
in a m1 method
www
in a m1 method
www
Javabykiran
Example 2:
package com.javabykiran;
public class VarargLab {
int add (int...i){
int sum=0;
for (int add : i ){
sum= sum+add;
}
return sum;
}
public static void main(String...args){
VarargLab vl =new VarargLab();
int add2=vl.add(1,2);
System.out.println(add2);
int add3=vl.add(1,2,3);
System.out.println(add3);
int add4=vl.add(1,2,3,4);
System.out.println(add4);
}
}
Output:
3
6
10
The static import feature lets fields and methods specified in a class as public static, and is used without naming the class in which the field is defined.
It is used for accessing static members of any class.
It is necessary to give the class name from where they came, if we don’t use this concept.
By using static import we can access static members directly, without giving the class name.
Static imports can be applied only to static members.
Example:
package com.javabykiran;
import static java.lang.System.out;
import static java.math.BigDecimal.TEN;
public class StaImpLab {
public static void main(String[] args){
out.println("www.javabykiran.com");
//no need to write System class
//Because it has already been imported.
out.println(TEN);
}
}
Output:
www.javabykiran.com
10
Annotations are a form of metadata which gives information about programs which is not a part of the current program.
It is a form of metadata.
It provides data about a program that is not part of the program itself. Annotations have no direct effect on the operation of the code they annotate. Java Annotation is a tag that represents the metadata i.e. attached with class, interface, methods or fields to indicate some additional information which can be used by java compiler and JVM.
There are many built in annotations present and we can also create custom annotations.
They have information for the compiler -
Annotations can be used by the compiler to detect errors or suppress warnings.
They send Compile-time and deployment-time processing -
Software tools can process annotation information to generate code, XML files, and so forth.
Runtime processing -
Some annotations are available to be examined at runtime.
Example:
package com.javabykiran;
class A {
void learn(){
System.out.println("C");
}
}
public class AnnotationLab extends A {
@Override
void learn(){
System.out.println("java by Kiran Sir");
}
//error void teach() method does not exist in the
//parent class.
/*@Override
void teach(){
}*/
public static void main(String[] args) {
AnnotationLab lab = new AnnotationLab();
lab.learn();
}
}
Output:
java by Kiran Sir