J2SE 5.0 corner

xiaoxiao2021-03-06  40

1, Dynamic Cast

Similar to the C Dynamic_CAST operator, the C # AS operator, Java 5.0 provides a secure Dynamic Cast feature, which is provided in the form of class libraries, and the type does not match, it is necessary to throw it. It greatly reduces usability:

Class.cast

Public T Cast (Object Obj)

Casts an Object to the class or interface represented by this

Class Object.

Parameters:

Obj - The Object To BE CAST

Returns:

The ObjectAfter 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 coordination

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 security agent

Similar to read-only agent Collectes.unmodifiablexxx (), synchronous proxy collections.synchronizedxxx (), J2SE 5.0 provides type of security agent: collections.checkedxxx ()

4, arrays.deepequals () is worthy of it in java.util

5, the emperor's "generic" new clothes

Plus a bunch of dazzling concepts, but is tired by the "Erase" implementation, the most practical function of personal use is only to avoid partial mandatory type conversion because of erase, so uncharged, only inheritance Appropriate simulation, completely killing all the idea of ​​automatic assignment according to the type of reference, because all the runtime calculations related to type parameters (Typevariable), such as forced type conversion, all want to want to be in generics The type method is called, the type parameter must be , a inference is In front of , there is no meaning ironic in front of , it is set to throw problems Solved to the compiler, but in addition to helping you, there is almost something that is in addition to the Typevariable information, and everything is not safe.

6, derived based on the type of return value

I don't know, it is not considered the feature brought about "Erase".

Example 1, Collectes.emptyxxx ()

EMPTYLIST

Public Static Final List EmptyList ()

Returns the Empty List (Immutable). This List is Serializable.This Example Illustrate The Type-Safe Way To Obtain An Empty List:

List s = collections.emptylist ();

Implementation Note: Implementations of this Method NEED NOT CREATE A SEPARATE

List Object for Each Call. Using this Method Is Likey to have compailable cost to using the like-named field. (Unlike THISMED, THE FIELD DOES NOT Provide Type Safety.)

Since:

1.5

See Also:

EMPTY_LIST

Example 2, one-step derivation

However, if it returns the value of the extensive parameter for another function, you must use a temporary variable to transition, it may be because it cannot be derived at the same time.

Public class conditionParser {private conditionParser () {} public static condition deserialize (String condition) {....}}

List > conditions = new arraylist > (); condition Safe = conditionParser.Deserialize (condition.Toxml ()); conditions.add (safe); // ok

List > conditions = new arraylist > (); conditiones.add (conditionParser.DeselIze)); // Error

7, type safe VARARGS

In fact, it is a shorthand form of an array, so it is a type of security, which has no convenience, and has not found some side effects.

8, serialversionuid

It is also used today to expose the intent, but also use Annotation to achieve a bit better than now, look at it in business data objects.

9. Object-oriented ENUM

Different from Enum in C , enum in J2SE 5.0 is an object-oriented implementation of the "UML Subclass" situation (although it looks like "limited instances": can implement the interface, can have a constructor, there is a method, Members, in addition to can't inherit and inherited, the constructor must be private, other and ordinary Java classes almost

(To Be Continue ...) (The Java Programming Language Notes)

转载请注明原文地址:https://www.9cbs.com/read-86200.html

New Post(0)