java new features


New Features of Jdk 1.5
1.      java.util.Scanner(jdk1.5) Class
    Scanner class is introduced in jdk1.5 version to take the Input from the user. This class is available in java.util package.
Scanner Class Methods
 Method
Returns
int nextInt()
Returns the next token as an int. If the next token is not an integer, InputMismatchException is thrown.
long nextLong()
Returns the next token as a long. If the next token is not an integer, InputMismatchException is thrown.
float nextFloat()
Returns the next token as a float. If the next token is not a float or is out of range, InputMismatchException is thrown.
double nextDouble()
Returns the next token as a long. If the next token is not a float or is out of range, InputMismatchException is thrown.
String next()
Finds and returns the next complete token from this scanner and returns it as a string; a token is usually ended by whitespace such as a blank or line break. If not token exists, NoSuchElementException is thrown.
void close()
Closes the scanner.
boolean hasNextLine()
Returns true if the scanner has another line in its input; false otherwise.

boolean hasNextInt()
Returns true if the next token in the scanner can be interpreted as an int value.

boolean hasNextFloat()
Returns true or false

         
import java.util.Scanner;
class ScannerDemo{
      public static void main(String[] args) {
                  Scanner sc=new Scanner(System.in);
                  System.out.println("enter ur eid :");
                  int eno=sc.nextInt();
                  System.out.println("enter ur ename :");
                  String name=sc.next();
                  System.out.println("enter ur esal :");
                  double sal=sc.nextDouble();
                  System.out.println(eno);
                  System.out.println(name);
                  System.out.println(sal);
      }
}
2.     New jdk 1.5 ‘for’ loop ( Enhanced for loop-’for each’)
The enhanced for-loop is a popular feature introduced with the Java  platform in version 5.0. Its simple structure allows one to simplify code by presenting for-loops that visit each element of an array/collection without explicitly expressing how one goes from element to element.
For-Each loop:
This for-each loop inducting jdk 1.5 versions. For-each loop is used only “1D-Array”.
          Syntax:   for(declaration : array name )
                           {
                             Stmts;
                            }
          class ForeachDemo {
          public static void main (String[] args) {
                  Char ch []={'a','b','c','d'};
                  for (char i:ch) {
                  System.out.println (i);
                   }
            }
            }
3.      Variable length of arguments
It is useful for function overloading, means same function with different args.Instead of declaring  M1(int) , M1(in,int)  ,M1(int,float)No of functions with different args, they have given this conceptEx:--Void m1(int … s)…  means any no of parameters of same type.Ex:--//variable length of arrays/*if we give elipses then we can send any type of datainto the related function args eventhough we have only one int argbut all should be of same dayatype*/class VarArgs {       void  m1(int ... s){                  System.out.println("i am in m1() method");      }      public static void main(String[] args) {                  System.out.println("Variable length of args");                  System.out.println("***********************");                  VarArgs obj=new VarArgs();         obj.m1(2,3);                  obj.m1(2,3,4);                  //obj.m1(1,2.2);      }}
4.     Generics
Java Generic methods and generic classes enable programmers to specify, with a single method declaration, a set of related methods or, with a single class declaration, a set of related types, respectively. Generics also provide compile-time type safety that allows programmers to catch invalid types at compile time. Using Java Generic concept we might write a generic method for sorting an array of objects, then invoke the generic method with Integer arrays, Double arrays, String arrays and so on, to sort the array elements. All generic method declarations have a type parameter section delimited by angle brackets (< and >) that precedes the method's return type.
A generic method's body is declared like that of any other method. Note that type parameters can represent only reference types not primitive types (like int, double and char).

Ex:

import java.util.*;
class  GenericsEx1{      public static void main(String[] args) {                  ArrayList <Integer> al=new ArrayList <Integer>();                  //al.add(new GenericsEx());compile time error                   al.add(2);                  //al.add(new Object());                  System.out.println(al);      }}
Ex:public class GenericMethodTest{   // generic method printArray                       
   public static < E > void printArray( E[] inputArray )   {         for ( E element : inputArray ){      
            System.out.printf( "%s ", element );         }         System.out.println();    }    public static void main( String args[] )    {        Integer[] intArray = { 1, 2, 3, 4, 5 };        Double[] doubleArray = { 1.1, 2.2, 3.3, 4.4 };        Character[] charArray = { 'H', 'E', 'L', 'L', 'O' };        System.out.println( "Array integerArray contains:" );        printArray( intArray ); // pass an Integer array        System.out.println( "\nArray doubleArray contains:" );        printArray( doubleArray ); // pass a Double array        System.out.println( "\nArray characterArray contains:" );        printArray( charArray ); // pass a Character array    }
}
Generic Classes:
A generic class declaration looks like a non-generic class declaration, except that the class name is followed by a type parameter section.
Ex:
public class Box<T> {
  private T t;
  public void add(T t) {
    this.t = t;
  }
  public T get() {
    return t;
  }
  public static void main(String[] args) {
     Box<Integer> integerBox = new Box<Integer>();
     Box<String> stringBox = new Box<String>();
      integerBox.add(new Integer(10));
     stringBox.add(new String("Hello World"));
     System.out.printf("Integer Value :%d\n\n", integerBox.get());
     System.out.printf("String Value :%s\n", stringBox.get());
  }
}
Output:--
Integer Value :10
String Value :Hello World               

No comments:

Post a Comment