JDK 5 Introduction (by Kingoal)

xiaoxiao2021-03-06  50

The greatest change in "JDK 5" is to simplify development using a model, for-Each loop, automatic packing / unpacking, enumeration, parameters, static introduction (imports) and annotations to simplify development, which is available at the language level More clearer, smaller and safer supports procedures.

Form, for-Each and automatic packing / unpacking

The model makes you define the type of collation, which is better to define a list (List), (define its content), just like String. For example, the following code uses a new For-Each syntax to describe the list of Timertasks in a collection while noticing the lack of type transforms and cycle syntax that is very easy to understand.

Void Cancellall (Collection

c)

{

For (Timertask Task: C)

{

Task.cancel ();

}

}

In 1.4 and 5.0, the bytecode is the same, but it is only to convert the code. I often hear some netizens say why: instead of IN? There are two aspects of the answer to this question, and the first in is already a keyword (for example, in system.in), it does not want to introduce new keywords. In JDK 5, just introduces a new keyword: ENUM.

Automatic packing: automatically convert int transform into Integer

Unpack: Automatically convert the Integer to int

For example, you can easily do the following:

Integer i = new integer (5);

Map map = new hashmap ();

Map.Put ("Result", i 1);

You will notice the above example, first convert the Integer type to int, then add the addition, and finally the box is Integer and put it in the Map. Cool, is it? ^ _ ^

JDK 5 simplifies the reflection function, class Class is more common (generified) --- foo.class is a type Class

. This makes it possible to safely reflect W / O conversion in the compile period. The following code returns an object and performs some necessary transformations.

Foo foo = foo.class.newinstance ();

When will I use a model?

Values ​​can be used at any time unless you run on the Java virtual machine before 5.0.

When do you use the for-Each loop?

You can use it at any time because it can beautify your code and make it easier to implement. Although it is the smallest new feature in 5.0, it is the favorite characteristic of people. But in the following cases, for-each:

l Delete elements in the collection (because there is no item)

l Modify the current SLOT of a group or list (because of INDEX)

l in multiple collections or arrays Iterate

ENUMS

JDK 5 supports enumeration types at the language level.

ENUM season {winter, spring, summer, fall}

Enumeration is comparable and serialized. The constant of the enumeration is the same as the normal constant.

Change parameters

There is a way to use any number of values ​​as a parameter to use an array. However, using VARARGS (change parameters) can be automatically completed and hidden in the specific process. James Gosling uses ... grammar. In general, VARARGS is generally the last parameter. In JDK 5, the MessageFormat.Format function has been rewritten using VARARGS.

Public static string format (String Pattern, Object ... arguments); string result = messageformat.format ("at {1, time} on {1, date}, there WAS {2} on planet"

"{0, Number, INTEGER}.", 7, new Date (),

"a disturbance in the force");

Use VARARGS to make reflection simpler

So you can use C.getMethod ("test"). Invoke (C.NewInstance ()) instead of using C.getMethod ("Test", New Object [0]). Invoke (C.NewInstance (), New Object [ 0])).

When will I use parameters?

1. If you design your own API, please use it.

2. When Benefit is forced, do not overload VARARGS methods

3. In the client, when API support: reflection, message formatting, Printf

Static introduction

The client often defines a static member (Math.pi) in the class. In order to avoid this, some programmers put some constants in the interface and realize it. BAD (constant interface reverse mode). This model is also incorrect in JDK --- java.util.jar. Static introduction can access static members without restrictions. All static properties, methods, and the like will be available after using static introduction.

Import static java.lang.math. *;

R = COS (PI * Theta);

When will I use static introduction?

It is best to use ---- Excessive use will make the program difficult to read. When it is possible to abuse inheritance, use it

Metadata

Use additional information to decorate the program. Comments do not affect the semantics of the program. But when using the tool or the library, it can affect the user's attitude. Ability to read from below, source code, class file, etc. Like @ deprecated, transient, etc. JDK 5.0 provides a more common metadata tool.

Why use metadata?

A lot of APIs need some sample files, like JAX-RPC, etc.

L Like a lot of APIs need to maintain "Side Files", like BeanInfo, deployment descriptor

l Many APIs use a famous pattern, like Junit

Metadata encourages the use of declarative programming styles - telling the computer what will do, now how to do it. The comment type declaration is the same as the interface declaration. Special annotations include tag annotations and single-element comments.

A new set of test tools in the newly released JDK1.5 / J2SE 5.

Performance Monitoring Tools JCONSOLE, JPS, JSTAT, JSTATD. At the same time, some new faults found and modify the tools can also be downloaded. These tools can display memory tables, generate stack tracking in the thread, and give a process or core print configuration information.

Monitoring, management tools

JCONSOL: J2SE Monitoring and Management Console --- A graphics tool for monitoring Java virtual machines with JMX. It can monitor local or remote Java virtual machines.

JPS: Java Virtual Machine Process Status Tools --- List the equipped virtual machine equipped with HotSpot Java on the target system

JSTAT: Java Virtual Machine Statistical Monitoring Tool - Additionally to a Java virtual machine equipped with HotSpot to collect and record performance statistics

JSTATD: JSTAT daemon for Java virtual machines - starts an RMI server program to monitor the creation and abort from each HotSpot Java virtual machine. And provide an interface to access the remote monitoring tool access.

Fault tool

l JINFO: Java configuration information. Print a given process or core file or the configuration information of the remote DEBUG server. l JSADebugd: Java Practical Agent debug daemon ---- Connect a process or a core file so that it runs like a debug server.

l JMAP: Java Memory Mapping ---- Print Shared Objects Memory Mappings or a given process or core object or detail of the remote debug server on the stack of memory

l JSTACK: Java Stack Tracking - Prints the Stack Tracking Information of the thread of a given process or core file or remote debug server

In addition, in JDK, there are many common standard tools:

Basic tools (Javaac, Java, Javadoc, Apt, AppletViewer, Jar, JDB, Javah, JavaP, Extcheck)

Safety tools (Keytool, Jarsigner, Policytool, Kinit, Klist, KTAB)

International Tools (Native2ASCII)

Remote Method Call (RMI) Tool (RMIC, RMIREGISTRY, RMID, SerialVer)

Java IDL and RMI-IIOP Tools (TNameserv, IDLJ, ORBD, ServerTool)

Java Deployment Tool (Pack200, Unpack200)

Java plugin tool (HTMLCONVERTER)

Java Web Start Tools (Javaws)

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

New Post(0)