New features of Java JDK1.5
1. Generic type:
List<string> STRs = new arraylist<string> ()//Specifies the type of deposit to the collection, which must be stored in String data when it is stored, otherwise the compiler will complain
2.for-each
For example, the above set we can iterate through the For-each, which is much simpler and clearer
for (String s:strs) {
System.out.println (s);
}
Note: When using For-each to traverse a collection, the set to traverse must implement the iterator interface
3. Automatic unpacking and Packing function
Copy Code code as follows:
What does that mean?
JDK1.5 defines a wrapper class for each of the basic data types. Make Basic data types in Java also have their own objects
For example: int-->integer,
Double--> Double,
Long--> Long,
Char--> Character,
Float--> Float,
Boolean--> Boolean,
Short--> Short,
BYTE--> Byte
Automatic Packaging: Converts a base type to an object, for example: int--> Integer
Automatic unpacking: Converts an object to the base data type, for example: Integer--> int
The problem that the collection cannot hold the basic data type before JDK1.5 can now be resolved.
4. Enumeration:
Enumerations are the attributes that are needed in a comparison that JDK1.5 introduces. The keyword is an enum
For example: Define an enumeration representing traffic lights
public enum myenum{
Red,green,yellow
}
5. Variable parameters
What does that mean? First, for example: Before JDK1.5, when we were going to pass multiple types of identical parameters for a method, we had two solutions, 1. Pass an array directly past, 2. How many parameters are passed.
For example:
public void Printcolor (String red,string green,string yellow) {
}
Or
public void Printcolor (string[] colors) {
}
So writing method parameters can achieve the results we want, but is this a bit of a hassle? Moreover, if the number of parameters is not certain, what should we do? The variable parameters provided by the Java JDK1.5 can solve this problem perfectly.
For example:
public void Printcolor (String ... colors) {
}
What do you mean by this definition? If the parameters are of the same type, you can use the form "type + three dots, followed by a parameter name." The advantage of this is that, as long as the parameter types are the same, there is no limit on whether to pass several parameters
Note: The variable parameter must be the last item in the argument list (the attribute applies to both the object and the base data type)
6. Static Import
Advantage: Use static import to make all static variables and static methods of the imported classes visible directly in the current class, using these static members without having to give their class names.
Disadvantage: Overuse can degrade code readability
7. Thread Concurrent Library
Thread Concurrent Library is the advanced feature of Java1.5, which contains the package: Java.util.concurrent
Including
1. Thread Mutex
Tool class Description: Lock,redwritelock
2. Thread Communication
Description: Condition
3. Thread pool
Executorservice
3. Synchronizing queues
Arrayblockingqueue
4. Sync Collection
Concurrenthashmap,copyonwritearraylist
5. Thread Synchronization Tool
Semaphore
There's a lot more (important) about the contents of the thread concurrency library, which is not listed here, and interested friends can look at the help documentation.
Ii. New characteristics of JDK1.6
1.Desktop class and Systemtray class
The former can be used to open the system default browser to browse the specified URL, open the system default mail client to the specified mailbox e-mail, open or edit the file with the default application (for example, with Notepad to open the file with txt suffix), with the system default printer to print the document , which can be used to create a pallet program in the system Tray area.
2. Using JAXB2 to map between objects and XML
JAXB is the acronym for the Java Architecture for XML binding, which transforms a Java object into an XML format and vice versa.
The mapping between objects and relational databases is called ORM, and the mapping between objects and XML can also be called OXM (Object XML Mapping). It turns out that JAXB is part of Java EE, and in JDK1.6, Sun puts it in the Java SE, which is what sun has always done. The JDK1.6 version of this JAXB is 2.0, compared to 1.0 (JSR 31), JAXB2 (JSR 222) identifies the classes and attributes to be bound with the new JDK5 feature annotation, which greatly simplifies the development effort. In fact, in Java EE 5.0, EJB and Web services also simplify development work through annotation. In addition, JAXB2 uses Stax (JSR 173) to process XML documents at the bottom.
In addition to JAXB, we can achieve the same function through XMLBeans and Castor.
3. Understanding Stax
StAX (JSR 173) is another API for processing XML documents in JDK1.6.0, in addition to DOM and sax.
The origins of StAX: in JAXP1.3 (JSR 206) There are two ways to work with XML documents: DOM (document Object Model) and sax (simple APIs for XML).
Because JAXB2 (JSR 222) and Jax-ws 2.0 (JSR 224) in JDK1.6.0 used Stax, Sun decided to add Stax to the Jaxp family and upgrade the JAXP version to 1.4 ( JAXP1.4 is a maintenance version of JAXP1.3). The JDK1.6 inside the JAXP version is 1.4. Stax is the streaming API for XML abbreviation, a pull mode parsing (pull-parsing) XML document Api.stax by providing an event-based iterator (iterator) API for programmers to control the parsing process of XML documents , the program iterates through the event iterator to handle each parsing event, which can be viewed as a program pull out, that is, a program that causes the parser to produce a parsing event and then processes the event, and then causes the parser to produce the next parsing event, so that it loops until it touches the document terminator. Sax is also based on an event-handling XML document, but it is resolved in a push mode, the parser resolves the entire XML document before it produces the parsing event, and then pushes the program to handle the events; The DOM takes the form of mapping the entire XML document to a single memory tree. This makes it easy to get the data of the parent and child nodes and sibling nodes, but if the document is large, it can have a significant impact on performance.
4. Using the compiler API
Now we can use the JDK1.6 Compiler API (JSR 199) to dynamically compile Java source files, Compiler API combined with reflection function can be implemented dynamically generate Java code and compile the code, a bit dynamic language characteristics.
This feature is useful for applications that need to be dynamically compiled, such as JSP Web server, when we manually modify the JSP, we do not want to restart the Web Server to see the effect, this time we can use the compiler API to implement dynamic compilation JSP , of course, now JSP Web server is also supporting JSP hot deployment, the current JSP Web server through the Runtime.exec or Processbuilder to invoke Javac to compile the code, this way requires us to produce another Process to do the compilation work, not elegant and easy to make code dependent on a specific operating system; The Compiler API provides a richer way to do dynamic compilation and Cross-platform, using an Easy-to-use standard API.
5. Lightweight HTTP Server API
JDK1.6 provides a simple HTTP server API, whereby we can build our own embedded HTTP server, which supports HTTP and HTTPS protocols, and provides a partial implementation of the HTTP1.1, which can be implemented by extending an existing HTTP Server API to implement, the programmer must implement the HttpHandler interface on its own, Httpserver will invoke the callback method of the HttpHandler implementation class to handle the client request, where we call an HTTP request and its response as an exchange, Packaged as a Httpexchange class, Httpserver is responsible for passing httpexchange to the HttpHandler implementation class's callback method.
6. Insert annotation Processing API (pluggable Annotation processing API)
The plug-in annotation processing API (JSR 269) provides a set of standard APIs to handle annotations (JSR 175)
In fact, JSR 269 is not just for annotation, I think the more powerful feature is that it builds a model of the Java language itself, which puts Method,package,constructor,type,variable, enums, The Java language elements, such as annotation, are mapped to types and elements (what is the difference between the two), thus mapping the semantics of the Java language into objects that we can see below the Javax.lang.model package. So we can use the APIs provided by JSR 269 to build a rich metaprogramming (metaprogramming) environment. JSR 269 is handled by Annotation processor during compilation rather than during run Annotation,annotation processor is equivalent to a compiler plug-in, so it is called insert annotation processing. If Annotation Processor processing annotation (executing the Process method) produces a new Java code, the compiler calls again annotation Processor, and if the second process has a new code generation, it calls the annotation Processor until no new code is generated. Each execution process () method is called a "round" so that the entire annotation processing process can be considered a sequence of round.
JSR 269 is designed primarily as an API for tools or containers. For example, we want to build a annotation unit test framework (such as testng) that uses annotation in a test class to identify the test methods that need to be performed during the test
7. Use Console to develop the console program
JDK1.6 provides a java.io.Console class for accessing character-based console devices. If your program is to interact with terminal under Windows CMD or Linux, you can use the console class. But we don't always get the console available, and whether a JVM has the available console depends on how the underlying platform and JVM are invoked. If the JVM is started in an interactive command line (such as Windows cmd), and the input output is not redirected to another location, an available console instance can be obtained.
8. Support for scripting languages
such as: Ruby,groovy,javascript.
9.Common annotations
Common annotations was originally part of the Java EE 5.0 (JSR 244) specification, and now Sun has put a portion of it in Java SE 6.0.
As the annotation metadata feature (JSR 175) is added to the Java SE 5.0, many Java technologies (such as Ejb,web Services) Configure run parameters (or support declarative programming) with annotation parts instead of XML files. such as the declarative transaction of EJB, if these technologies define their own otations for common purposes, it is obviously a bit repetitive, so defining a set of public annotation for other related Java technologies is valuable, avoiding duplication of construction while also ensuring that Java The consistency of various technologies between SE and Java EE.
Below is a list of 10 annotations Common annotations Annotation Retention Target Description generated in Common Annotations 1.0 Sourceannotation_type,constructor,field,local_variable,method,package,parameter,type is used to annotate the generated source code resource Runtime Type,method,field is used to annotate the resources on which they depend, and the container injects external resource dependencies, with a field-based injection and a setter-based injection of two ways the resource Runtime type simultaneously annotated multiple external dependencies, The container will have all of these external dependency injection postconstructruntime methods annotated as the container injects all dependencies to run after the initialization of the dependency injection, only one method can be labeled as Postconstruct Predestroy Runtime method when an instance of an object is to be deleted from the container, the callback method to be executed is annotated as Predestroy RunAs Runtime type is used to annotate what security role is used to perform the annotated class. This security role must be consistent with the container role. The rolesallowed Runtime Type,method is used to annotate the security roles that are allowed to perform the annotated class or method, which must be the Permitall Runtime TYPE consistent with the container security role, method allows all roles to perform annotated classes or methods Denyall Runtime Type,method does not allow any role to perform the annotated class or method, indicating that the class or method cannot run in the Java EE container declareroles Runtime Type is used to define security roles that can be validated by the application, typically using IsUserInRole to verify security roles.
Attention:
1.rolesallowed,permitall,denyall cannot be applied to a class or method at the same time the Rolesallowed,permitall,denyall on the method will overwrite the rolesallowed that is marked on the class. Permitall,denyallrunas,rolesallowed,permitall,denyall and Declareroles have not yet been added to Java SE 6.0 to handle the above annotations work is done by Java EE containers, the Java SE6.0 only contains the first five annotations definitions classes in the table above, and does not contain the engine that handles these annotations, which can be done by the pluggable Annotation processing API ( JSR 269) to do.
Compared with the new features of 1.6, 1.7 of the new features more exciting for us, because it is we have long awaited and can be seen to touch.
Iii. New characteristics of JDK1.7
1. Binary face value
In Java7, the value of the Shaping (Byte,short,int,long) type can be represented by a binary type, which needs to be preceded by OB or OB when using binary values, for example:
int a =0b01111_00000_11111_00000_10101_01010_10;
Short B = (short) 0b01100_00000_11111_0;
2. Digital variable support for the slide line
JDK1.7 can add a drop line to a variable of a numeric type.
But there are a few places that can't be added.
1. Beginning and end of numbers
2. Before and after the decimal point
3. F or L ago
For example:
int num = 1234_5678_9;
float num2 = 222_33f;
Long num3 = 123_000_111l;
3.switch Support for string
There's been a Da Guo before? Why can C # java but not? Yes, but there's JDK1.7, and then Java is okay.
For example:
String status = "Orderstate";
Switch (status) {case
"Ordercancel":
System.out.println ("Cancellation of Order");
break;
Case "ordersuccess":
System.out.println ("successful booking");
break;
Default:
System.out.println ("status Unknown");
4.try-with-resource
Try-with-resources is a try declaration that defines one or more resources, which is the object that the program needs to close after it has finished processing it. Try-with-resources ensure that each resource is closed after processing is complete.
Resources that can be used with try-with-resources are:
Any object that implements the interface java.io.Closeable the Java.lang.AutoCloseable interface.
For example:
public static string Readfirstlinefromfile (string path) throws IOException {
try (bufferedreader br = new Bufferedread ER (new FileReader (path)) {return
br.readline ();
}
}
In Java 7 and later versions, BufferedReader implements the Java.lang.AutoCloseable interface.
Because BufferedReader is defined in the Try-with-resources declaration, it is automatically turned off regardless of whether the try statement is normal or abnormal. Before Java7, you need to use the finally block to turn off the object.
5. Catch multiple exceptions and throw back the exception with improved type checking
For example:
public static void A () {
try {
BufferedReader reader = new BufferedReader (New FileReader (""));
Connection con = null;
Statement stmt = Con.createstatement ();
} catch (IOException | SQLException e) {
//Catch multiple exceptions, E is the final type of
e.printstacktrace ();
}
Advantage: Handling multiple exceptions with a catch is smaller and more efficient than using multiple catch each to handle an exception-generated byte code.
6. Create generic Time type inference
As long as the compiler can infer the type parameter from the context, you can replace the generic parameter with a pair of empty angle brackets <>. This pair of parentheses is privately referred to as a diamond (diamond). Before Java SE 7, you declare a generic object like this
list<string> list = new arraylist<string> ();
And after the Java SE7, you can do this.
list<string> list = new arraylist<> ();
Since the compiler can infer a type parameter from the front (List), the following ArrayList can be used without writing generic arguments, with only a pair of empty angle brackets. Of course, you have to take the "diamond" <>, or there will be a warning.
The Java SE7 only supports limited type inference: only the parameterized type of the constructor is declared prominently in the context, you can use type inference, otherwise not.
list<string> list = new arraylist<> ();
List.add ("A");
This can't be
list.addall (new arraylist<> ());
This can be
list< extends string> list2 = new arraylist<> ();
List.addall (LIST2);
7. (none)
8. New tools to access environmental information
For example:
File System.getuserhomedir ()//Current User directory
file System.getuserdir ()//The directory where the Java process started 5
file System.getjavaiotempdir ()//IO Temp folder
file System.getjavahomedir ()//JRE installation directory
9. Subtraction of security
For example:
int Math.safetoint (Long value)
int math.safenegate (int value)
long math.safesubtract (long value1, int value2) C3/>long math.safesubtract (Long value1, long value2)
int math.safemultiply (int value1, int value2)
long Math.safemultiply (long value1, int value2) long
math.safemultiply (long value1, long value2)
long Math.safenegate (Long value)
int math.safeadd (int value1, int value2)
long Math.safeadd (long value1, int value2)
long Math.safeadd (long value1, long value2)
int math.safesubtract (int value1, int value2)
Well, so far, so much. See later, I will add to add.
Be aware that if you are unsure about the features of your previous JDK version, you may not be able to use them.