Java Generic Insights

Source: Internet
Author: User
Tags java se

According to Baidu Encyclopedia Description:

Generics are a new feature of the Java SE 1.5, the essence of which is a parameterized type, which means that the data type being manipulated is specified as a parameter. This type of parameter can be used in the creation of classes, interfaces, and methods, called generic classes, generic interfaces, and generic methods, respectively. The benefits of introducing generics into the Java language are simple and safe.
before the Java SE 1.5, without generics, the argument is "arbitrary" by reference to type object, and the disadvantage of "arbitrariness" is to do explicit coercion of type conversions, which require the developer to be able to predict the actual parameter types. In the case of coercion type conversion errors, the compiler may not prompt for an error and the exception occurs at run time, which is a security risk.
the benefit of generics is to check type safety at compile time, and all casts are automatic and implicit to increase the reuse rate of code.

Overall:

1. Type safety
The main goal of generics is to improve the type safety of Java programs. By knowing the type limitations of variables defined using generics, the compiler can validate the type hypothesis at a much higher level. Without generics, these assumptions exist only in the programmer's mind.



2. Eliminate mandatory type conversions
A by-product of generics is the elimination of many coercion type conversions in the source code. This makes the code more readable and reduces the chance of error.

3. Potential performance gains
Generics are possible for larger optimizations. In the initial implementation of a generic, the compiler will force type conversions (without generics, programmers will specify these coercion type conversions) into the generated bytecode. But the fact that more types of information can be used in compilers is possible for future versions of JVM optimizations.


common places to use in projects:

A. Use generics on top of class:

public class Classgeneric<t> {
	private T t;
	Private class<t> Entityclass; 
	Public Classgeneric () {
		Type gentype = This.getclass (). Getgenericsuperclass ();
        Type[] params = ((Parameterizedtype) gentype). Getactualtypearguments ();  
        This.entityclass = (Class) params[0];
	}
	Public T Gett () {return
		t;
	}
	public void sett (T t) {
		this.t = t;
	}

}

A generic variable defined above a class can be used above a member property or used on a method. Because generics are only valid at compile time and are erased after compilation, you need to inherit the class.

For example:

public class Generic extends classgeneric<tearsure> {public
	void fun () {
		tearsure tearsure = This.gett () ;
	}
}

A. Use generics on member variables:

public class Membergeneric {public
	<T> void Fun (T t) {
		System.out.println (t);
	}
}


Define generics above the method.


Summary: Generics are only one type of variable, somewhat similar to object, but unlike object, generics he can intuitively reflect the nature of this type.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.