Java 5 Features examples

 Posted On  | Yashwant Chavan 

Java 5 add many features Like Generics, Type-Safe Enumerations, Automatic Boxing and Un - Boxing, Annotations, For/In loops, and Static Imports and so on, We will Each Feature with the help of below examples

Java For Each Loop

Early days looping are redundant and annoying. You need to define the the start point and end point to control the loop behavior. But new loops are pretty cool over the old one. In below example defined String[] array , and we are going to looping it using java5 syntax. It is applicable to List, Map, Queue, Set, and other collections Arrays.

package com.technicalkeeda;

public class LoopTest {
 public static void main(String[] args) {
  String[] counts = { "one", "two", "three" };
  for (String str : counts) {
   System.out.println(str);
  }
 }
}

Java AutoBoxing

Before 1.5 you can not put primitive type values into collections. Collections can only hold object references. So Java 1.5 introduce Auto boxing which converts the primitive type into respected Wrapper class object. e.g. int become Integer Object automatically. The autoboxing and unboxing feature automates the process. The performance of the resulting list is likely to be poor, as it boxes and unboxes. So suggestion is do not use it extensively for the larger collection.

package com.technicalkeeda;

import java.util.ArrayList;
import java.util.List;

public class AutoboxingTest {
 
 public static void main(String[] args) {

  List list = new ArrayList();
  list.add(10);
  list.add(10.5);

  for (Object obj : list) {
   System.out.println(obj);
  }

 }

}

Java Variable Length Argument List

Varargs is used to take arbitrary number parameters to the method. Three dots (...) indicates after Object Type is method took number of parameters. Like in below display() method signature, having String type of Varargs list. In First display() method call we pass only one parameter, while in second call we pass two parameters, same way you can pass arbitrary number of parameters while calling the method.

package com.technicalkeeda;

public class VarargsTest {

 public static void main(String[] args) {
  VarargsTest test = new VarargsTest();
  test.display("Apple");
  test.display("Cat", "Dog");
  test.display("One", "Two", "Three");
 }

 public void display(String... parameters) {
  for (String str : parameters) {
   System.out.println("Paramters:-" + str);
  }
  System.out.println("");
 }

}

Java Static Import

Java 5 added new static import concept. To access the static members, it is necessary to specify from which class it comes from. In below example abs method comes from Math class. So prior Java 5 we need to specify the class name along with static member. But the new approach provides a facility to import it only once and then directly use that static member in code, static member may be method or variable.

int value = Math.abs(-100);

static import in Java 5


import static java.lang.Math.abs;

public class Test {

 public void testAbs() {
  System.out.println(abs(-100));
                System.out.println(abs(-400));

 }

 public static void main(String[] args) {
  new Test().testAbs();
 }
}


Promote your Blog, Website, or services on technicalkeeda.com, My paypal id: yashwantchavan@gmail.com

Payment will be accepted through paypal only.


© technicalkeeda.com 2017

 |  Find us on Google+ |  Rss Feed

Loaded in 0.0197 seconds.