New features of J2SE1.5 (one)

zhaozj2021-02-17  51

New features of J2SE1.5

J2SE's next version name is "Tiger" does not lose its compatibility if the programmer's code is clearer, shorter, safer and easier to develop. Please simply talk about the advantages of J2SE 1.5.

The new Java language features are based on a purpose: try to make them use some of the same syntax habits and provide semantic support. That is, the previous programmer needs to write a lot of semantic programming formats in the new program to be made by the compiler.

What change is the most difficult for programmers? Those changes are not facing the programmer?

In general, there is nothing to make people feel difficult, and the change in generic programming may be large. Use generic programming requires additional declaration information when declaring. For example: list word = new arraylist (); need to be replaced: list words = new arraylist ();

One advantage of this is that if you insert an array data type is not a string, you can discover and solve this bug when you compile. If this bug is not discovered when it is compiled, this bug cannot be discovered when compiling. The program is incorrect. Another benefit is: You don't have to worry about the elements in the collection beyond the scope:

String Title = (String) Words.get (i)). TouPpercase ();

use:

String title = words.get (i) .touppercase ();

Can you simply introduce these six aspects of these J2SE 1.5 changes?

Ok,

Wide Programming - Provides a safety type check at the compilation of the collection object.

Enhances for loops - programming is easier, removes problems that have made iterative errors.

Packing / Unpack-Original Type (int) and package types (Integer) are easier.

Type Safety Enumeration - Provides the most common type of security enumeration mode. (Effective Java, Item 21)

Static Import Static Import - Lets You Avoid Qualifying Static Members with Class Names, WITHOUT The Shortcomings of The Constant Interface Antipattern (Effective Java, Item 17).

Metadata - Avoid writing code for describing information, implementing the mode of "declaration" programming. The programmer declares what needs to do, then by the related tools to complete the specific work.

Filter an element in a collection, what is the difference between the practices in J2SE1.5?

The current practice is:

/ **

* Remove a 4-character element from a specified collection.

* /

Static void Expurgate (Collection C) {

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

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

IF (s.length () == 4)

I.Remove ();

}

}

The above code, some defects, may be wrong during operation. For example: if there is a StringBuffer type data in a collection.

You can do this later:

Static void ExpurGate (Collection C) {

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

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

}

Let's talk about the enhanced for loop!

An iteration of the elements in a collection, the original approach is panicked. In most cases in J2SE 1.5, you don't need to use iTerate to traverse a collection. Enhanced For loop, let the compiler to complete the specific iteration work. For example: void can (collection c) {for (iTerator i = c.iterator (); I.hasnext ();) {TIMERTASK TT = (Timertask) i.next (); tt.cancel ();}} can now this way:

Void Cancelall (Collection C) {

For (Object O: C)

(TIMERTASK) o) .Close ();

}

Note: The colon above, it represents: IN. An alternative to C # or natural is: foreach and in. But considering compatibility, we don't do that.

What is the result after generic programming and enhanced for?

The code in the above example can be represented by the following code:

Void Cancelall (Collection C) {

For (Timertask Task: C)

Task.cancel ();

}

What is a packing?

Everyone knows that there are two data types in the Java language: some are the basic data types, and others are object reference types. Basic data types cannot be placed directly into the collection unless the corresponding type conversion is made. This conversion is very boring.

According to an example: The key of the MAP data type is used to store words, and Value is used to store the number of times of repetition. This is a small program that calculates the frequency of words.

PUBLIC CLASS FREQ {

Private static final integer one = new integer (1);

Public static void main (string args []) {

Map m = new trends ();

For (int i = 0; i

Integer Freq = (Integer) M.GET (Args [i]);

M.PUT (args [i], (freq == null? one:

New Inteder (freq.intvalue () 1))))))))

}

System.out.println (M);

}

}

Here is the code after packing, generic, and enhanced for loop:

PUBLIC CLASS FREQ {

Public static void main (string args []) {

Map m = new trememap ();

For (String Word: Args)

M.PUT (Word, M.Get (Word) 1);

System.out.println (m);

}

}

Note: The above program assumes that the value is 0 when the unbox is null.

of two:

http://www.9cbs.net/develop/read_article.asp?id=18442

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

New Post(0)