Corner of j2se 5.0

Source: Internet
Author: User

1. Dynamic cast

Similar to the dynamic_cast <t *> operator of C ++ and the as operator of C #, Java 5.0 provides a secure dynamic cast function. The difference is that it is provided in the form of a class library, in addition, an exception is thrown when the types do not match, greatly reducing the availability:

Class. Cast

public T cast(Object obj)
Casts an object to the class or interface represented by this ClassObject.

Parameters:
obj-The object to be cast
Returns:
The object after casting, or null if obj is null
Throws:
ClassCastException-If the object is not null and is not assignable to the type T.
Since:
1.5

2. Return Value covariant

Interface someinterface {
Object get ();
}

Class covariantimpl implements someinterface {
Public String get (){
Return "covariant ";
}
}

Public class testuntitled2 extends testcase {
Public void testcovariant (){
Someinterface OBJ = new covariantimpl ();
Assert. assertequals ("covariant", obj. Get ());
}
}

3. Type-safe proxy

Similar to the read-only proxy collections.Unmodifiablexxx (),Synchronization proxy collections.Synchronizedxxx (), J2se 5.0 provides type-safe Proxy: collections.Checkedxxx ()

4, arrays. deepequals ()

It is stored in the Java. util package.
 

5. Emperor's "generic" new clothes

  • A bunch of dazzling concepts are added, but the implementation of the "erasure" method is tiring. Currently, the most practical function used by individuals is to avoid some forced conversions.

  • Because it needs to be erased, it cannot be made special. We can only use inheritance and instantiation simulation to completely eliminate all ideas for automatic allocation based on the real parameter type.

  • All runtime computations related to the type parameter (typevariable), such as forced type conversion, are meaningless because they need to be erased.

  • To call a Business Method of the type parameter (typevariable) in a generic class method, the type parameter must be <? Extends somesuper>, an inference is <? Extends markerinterface> meaningless before <markerinterface>

  • The irony is that the problem is thrown to the compiler to solve, but the compiler has nothing to do except to help you to compress some typevariable information.

  • However, after class. forname (), everything is insecure.

6. Type derivation based on return values

I don't know if it is a feature of the "erasure" method.

Example 1: collections. emptyxxx ()

Emptylist
public static final <T> List<T> emptyList()
Returns the empty list (immutable). This list is serializable.

This example extends strates the type-safe way to obtain an empty list:

     List<String> s = Collections.emptyList(); 

Implementation Note: implementations of this method need not create a separateListObject for each call. Using this method is likely to have comparable cost to using the like-named field. (unlike this method, the field does not provide type safety .)

 

Since:
1.5
See also:
EMPTY_LIST

Example 2: One-Step Derivation

However, if the returned value is used for the generic parameters of another function, you must use a temporary variable to perform a transition, probably because two-step derivation is not allowed.

Public class conditionparser {
Private conditionparser (){
}

Public static <t> condition <t> deserialize (string condition ){
....
}
}

List <condition <t> conditions = new arraylist <condition <t> ();
Condition <t> safe = conditionparser. deserialize (condition. toxml ());
Conditions. Add (SAFE); // OK
 

List <condition <t> conditions = new arraylist <condition <t> ();
Conditions. Add (conditionparser. deserialize (condition. toxml (); // Error

 

7. Type-safe varargs

It is actually a short form of array, so it is type-safe and convenient, while no side effects have been found

8, serialversionuid

Today, we have also used implementation to expose our intent, and annotation to achieve this, which is a little more pleasing to the eye. We watched it appear in the business data object.

9. Object-oriented Enum

Unlike Enum in C ++, Enum in j2se 5.0 is an object-oriented implementation that complies with the "UML finite subclass" situation (although it looks like a "finite instance"): it can implement interfaces, you can have constructors, methods, and Members. Except that they cannot be inherited or inherited, constructors must be private. Other constructors are similar to common Java classes.

(To be continue ...)
(The Java programming language notes)

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.