à®®ேலுà®®் பல புதிய டெக்னாலஜி பற்à®±ி தெà®°ிந்து கொள்ள, எங்கள் "டெக் டுப் தமிà®´் நாடு" யூட் டூஃப் சேனலை SUBSCRIBE செய்து கொள்ளவுà®®்.

Breaking

Sunday 8 October 2017

Advanced JAVA 8


Advanced JAVA 8

Generics

A class or interface that operates on parameterized type is called Generic. Generics was first introduced in Java5. Now it is one of the most profound feature of java programming language. It provides facility to write algorithm independent of any specific type of data. Generics also provide type safety.

Using Generics, it becomes possible to create a single class or method that automatically works with all types of data(Integer, String, Float etc). It expanded the ability to reuse code safely and easily.

Example of Generic class

class Gen 
{
 T ob;     //an object of type T is declared
 Gen(T o)  //constructor
 {
  ob = o;
 }
 public T getOb()
 {
  return ob;
 }
}

class Test
{
 public static void main (String[] args)
 {
  Gen <Integer> iob = new Gen(100);     //instance of Integer type Gen Class. 
  int  x = iob.getOb();
  System.out.println(x);
  Gen <String> sob = new Gen("Hello");  //instance of String type Gen Class.
  String str = sob.getOb();
 }
}

Output :
100
Hello

Generics Work Only with Objects.

You cannot use primitive datatype such as int or char etc with Generics type. It should always be an object.

Gen< int> iob = new Gen< int>(07);    //Error, can't use primitive type

Generics Types of different Type Arguments are never same.

Reference of one generic type is never compatible with other generic type unless their type argument is same. In the example above we created two objects of class Gen, one of type Integer, and other of type String, hence,

iob = sob;    //Absolutely Wrong

Generic Methods

You can also create generic methods that can be called with different types of arguments based on the type of arguments passed to generic method, the compiler handles each method.

Example of Generic method

class GenTest
{
 static  void display (V v, T t)
 {
  System.out.println(v.getClass().getName()+" = " +v);
  System.out.println(t.getClass().getName()+" = " +t);
 }
 public static void main(String[] args)
 {
  display(88,"This is string");
 }
}

Output :
java.lang.Integer = 88
Java.lang.String = this is string

Generic Constructors

It is possible to create a generic constructor even if the class is not generic.

Example of Generic Constructor

class Gen
{
 private double val;
 extends
Number> Gen(T ob) { val=ob.doubleValue(); } void show() { System.out.println(val); } } class HelloWorld { public static void main(String[] args) { Gen g1 = new Gen(100); Gen g2 = new Gen(121.5F); g1.show(); g2.show(); } } Output : 100.0 121.5


Generic Interface

Like classes and methods, you can also create generic interfaces.

interface MyInterface < T >
{ .. }


Generic Bounded type Parameter

You can also set restriction on the type that will be allowed to pass to a type-parameter. This is done with the help of extends keyword when specifying the type parameter.

< T extends Number >

Here we have taken Number class, it can be any wrapper class name. This specifies that T can be only be replaced by Number class data itself or any of its subclass.

Generic Method with bounded type Parameters

class Gen
{
 static extends
Number> void display(T t, V v) { System.out.println(v.getClass().getName()+" = " +v); System.out.println(t.getClass().getName()+" = " +t); } public static void main(String[] args) { // display(88,"This is string"); display ("this is string",99.0); } } Output : java.lang.Double = 99.O java.lang.String = This is string

1. Type V is bounded to Number type and its subclass only.
2.If display(88,"This is string") is uncommented, it will give an error of type incompatibility, as String is not a subclass of Number class.

No comments:

Post a Comment

Hai , Post your comment . (required, Bugs, Errors )

You need to see


SUBSCRIBE NOW



Backlinks : Website Design Company in Coimbatore
Website Design Company in Tamil nadu
Website Design Company in Karur
Website Design Company in Erode
Website Design Company in ooty
Website Design Company in theni
Website Design Company in thoothukudi
Website Design Company in Chennai
Website Design Company in Sathy
Website Design Company in Coimbatore Tamilnadu
Cheap Website Design Company in Coimbatore
Adsense Website Design Company in Coimbatore
ecommerce Website Design Company in Coimbatore
school Website Design Company in Coimbatore
Academy Website Design Company in Coimbatore
Test Website Design Company in Coimbatore
Best Website Design Company in Coimbatore
Adsense Website Design Company in Coimbatore
Adsense Website Design Company in Tamil nadu
Adsense Website Design Company in Karur
Adsense Website Design Company in Erode
Adsense Website Design Company in ooty
Adsense Website Design Company in theni
Adsense Website Design Company in thoothukudi
Adsense Website Design Company in Chennai
Adsense Website Design Company in Sathy
Free SEO Tools online
Free SEO Tools online Website
Free SEO Tools online Plagiarism
Free SEO Tools online article rewriter tool
Free SEO Tools online article reading and writing criticism
Free SEO Tools online seo backlink
Free SEO Tools online Free backlink maker
Free SEO Tools online seo backlink strategy
Free SEO Tools online content writting
360 help SEO Tools online Plagiarism checker
360 help SEO Tools online seo backlink
360 help SEO Tools online Free backlink maker
360 help SEO Tools online content writting
360 help SEO Tools online article rewriter tool
360 help SEO Tools
360 help SEO Tools India
360 help SEO Tools Tamil
360 help SEO Tools website