J2SE1.5 Easy New Language Characteristics: Joshua Bloch Interview (2)

zhaozj2021-02-16  48

Can you summarize these six improvements?

Well, I made a brief address:

Pan generic - provides the compile period type of Collection operation and avoids the hardships of type conversion.

For cycle enhances Enhanced for loop - Avoid using iterators that are easy to cause errors.

Auto-put / automatically remove autoboxing / unboxing - Avoid hardships between the basic types (such as int) and packaging types (such as Integer).

Type Safety Enumeration TypeSafe Enums - provides the advantages of type security ((Effective Java, Item 21)), but there is no lengthy and error-free code.

Static import static import - allows you to avoid the prefix class name; and avoid the shortcomings of constant interfaces (Effective Java, Item 17).

Metadata - let you avoid writing sample code. You make tags in the program and then the tool will automatically generate sample code. This will lead to a declarative programming, what you should do, then the tool will generate the code to complete this task.

If you follow the new specification and start using generics, what is the difference between using a collection of collections and a collection of boatouts?

We generally use the collection:

/ **

* Elements of all lengths 4 are removed in a collection composed of String

* The collection of incoming must be composed of String

* /

Static void Expurgate (Collection C) {

Iterator i = C.ITERATOR (); I.hasnext ();) {

String s = (string) i.next ();

IF (s.length () == 4)

I.Remove ();

}

}

This type of conversion is not perfect, and more importantly, the program may have an error in runtime. Assuming that users are inseparable from a collection of StringBuffer instead of the String described in the comment, there may be unexpected occurrences. Note said that the client must pass a collection of string, but it is not required to meet the compiler when compiling.

The following is the same code using a generic example:

/ **

* Elements of all lengths 4 are removed in a collection composed of String

* /

Static void ExpurGate (Collection C) {

For (Iterator i = C.Iiterator (); I.hasNext ();)

IF (i.next (). Length () == 4)

I.Remove ();

}

It can be seen from the signature of the code now that the input parameter must be a collection that is only consisting of String. If the customer tries to pass in a collection of StringBuffer, the program will not be compiled. And please note that there is no type conversion in our code. It is only a short line, and it will be very clear when reading the code using generic collection.

Please tell us about "For"?

We can use more elegant methods to traverse a collection. When you usually traverse the collection, you are just taking the elements, rarely use other methods. "For Enhance" allows the compiler instead of you manage your iterator. For example, this is a collection that uses an iterator traversed by Timetask:

Void Cancelall (Collection C) {

Iterator i = C.ITERATOR (); I.hasnext ();) {

TIMERTASK TT = (Timertask) i.next ();

Tt.cancel ();

}

}

Now we rewrite this method with "for enhance": Void Cancelall (Collection C) {

For (Object O: c)

(TIMERTASK) o) .Close ();

}

When you read this statement, the semicolon reads "in". If we use two new keywords Foreach and IN will be more natural, but introducing new keywords will be more troublesome. For example, if we exactly use these two words as a flag, then the new compilation system will destroy our original code. Our method is to reserve the basic expansion language of compatibility.

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

New Post(0)