Differences between JDK versions

Source: Internet
Author: User

New Features of jdk1.5:
1. Generic
2. Automatic packing/unpacking
3 For-each
4 static Import
5. Variable Length Parameter

1. Generic (avoid running errors caused by forced type conversion)
For example:
Arraylist list = new arraylist ();
List. Add (New INTEGER (3 ));
List. Add (New INTEGER (4 ));
Int I = (integer) (list. Get (0). parseint ();
Very troublesome
Arraylist <integer> List = new arraylist <integer> ();
List. Add (New INTEGER (3 ));
List. Add (New INTEGER (4 ));
Int I = list. Get (0). parseint ();

2. Automatic packing/unpacking

The last sentence in the above example can be changed:
Int I = list. Get (0 );
Because explicit conversion is not required between the original type and the corresponding packaging class

3 For-each
Loop Enhancement
Int A [] = {...}; // Initialization
For (int I:)
{
......
}
Do not use the previous I = 0; I <A. length; I ++

4 static Import
Previously called Java. Math
Math. SQRT ();
Now static import java. Lang. Math. SQRT;
Then SQRT ();
This is equivalent to the method in your own class.

5. Variable Length Parameter
Int sum (Int... intlist)
{
Int I, sum;
Sum = 0;
For (INT I = 0; I <intlist. length; I ++)
{
Sum + = list [I];
}
Return sum;

}
Has any parameter, think of it as an array

New Features of jdk6.0

Enhanced for loop statements
To iterate collections and arrays, the enhanced for loop provides a simple and compatible syntax. There are two points worth mentioning:
1. in a loop, the initialization expression is calculated only once. Int expression

Unreinforced:
Int sum = 0;
Integer [] numbers = computenumbers ();
For (INT I = 0; I <numbers. length; I ++)
Sum + = numbers [I];

Enhanced:
Int sum = 0;
For (INT number: computenumbers ())
Sum + = number;

Limitations
The iterator or subscript cannot be accessed during an enhanced for loop iteration.
See the following example:
For (INT I = 0; I <numbers. length; I ++ ){

If (I! = 0) system. Out. Print (",");

System. Out. Print (numbers [I]);

}

This is another example:
For (iterator <integer> it = n. iterator (); it. hasnext ();)

If (it. Next () <0)

It. Remove ();

Note
Annotation processing is a big topic. Because this article only focuses on the core language features, we do not intend to cover all its possible forms and traps. We will discuss the built-in annotations (suppresswarnings, deprecated and override) and the limitations of General annotation processing.

Suppress warnings
This annotation disables the class or method-level compiler warning. Sometimes you know better than the compiler,CodeYou must use a denied method or perform actions that cannot be statically determined to be type-safe:
@ Suppresswarnings ("deprecation ")

Public static void selfdestruct (){

Thread. currentthread (). Stop ();

}

This may be the most useful part of built-in annotations. Unfortunately, javac 1.5.0 _ 04 does not support it. But 1.6 supports it, and sun is trying to port it back to 1.5.
Eclipse 3.1 supports this annotation, and other ides may also support it. This allows you to completely free code from warnings. If a warning occurs during compilation, you can confirm that you just added it to help you view the code that may be insecure. With the addition of generics, it will be easier to use.

Deprecated
Unfortunately, deprecated is not that useful. It was originally intended to replace the @ deprecated javadoc tag, but because it does not contain any fields, there is no way to recommend what the deprecated class or method users should use as a substitute. In most usage cases, the javadoc tag and the annotation are required at the same time.

Override
Override indicates that the methods annotated by it should override the methods with the same signature in the superclass:
@ Override

Public int hashcode (){

...

}

Looking at the example above, if "C" is not capitalized in hashcode, there will be no errors during compilation, but this method cannot be called at runtime as expected. By adding the override tag, the compiler will prompt whether it actually executes the rewrite.
This is also helpful when the superclass changes. If a new parameter is added to the method and the method itself is renamed, The subclass cannot be compiled because it no longer overwrites anything of the superclass.

Other annotations
Annotations are useful in other scenarios. Annotations run very well in frameworks such as EJB and web services, especially when you add sample code, instead of directly modifying the behavior but enhancing the behavior.
Annotations cannot be used as pre-processors. Sun's design specifically prevents modifying the class bytecode completely due to comments. In this way, you can correctly understand the results of the language, and tools such as IDE can also perform in-depth code analysis, refactoring, and other functions.
Note is not a silver bullet. At the first time, people tried various techniques. See the following suggestions from others:
Public class Foo {

 

@ Property

Private int bar;

 

}

The idea is to automatically create the getter and setter methods for the private field bar. Unfortunately, this idea has two failures: 1) It cannot run, 2) It makes the code hard to read and process. It cannot be implemented, because Sun has previously mentioned that it is particularly blocked from modifying the classes with comments.
Even if possible, it is not a good idea because it makes the code less readable. The person who saw this code for the first time will not know how the annotation was created. In addition, annotations are useless if you need to perform some operations within these methods in the future. In short, do not try to use comments to do things that can be done by conventional code.

Enumeration
Enum is very similar to the public static final int statement, which has been used as the enumerated value for many years. The biggest and most obvious improvement to Int Is type security-you cannot mistakenly use one type of enumeration to replace another type, which is different from Int, all int values are the same for the compiler. Except for a few exceptions, Enum instances should be used to replace all enumerated int structures.
Enumeration provides some additional features. The two classification classes enummap and enumset are specifically implemented for the standard set of enumeration optimization. If you know that a set only contains enumeration types, use these special sets instead of hashmap or hashset.
In most cases, you can use Enum to insert and replace all public static final int values in the code. They are comparable and can be imported statically, so references to them seem equivalent, even for internal classes (or internal enumeration types ). Note: When comparing enumeration types, the statements that declare them indicate their sequential values.

"Hidden" static method
The two static methods appear in all Enumeration type declarations. Because they are static methods on enumeration subclasses, rather than the enum method, they are not found in javadoc of Java. Lang. enum.
The first is values (), which returns an array of all possible values of the enumeration type.
The second is valueof (). An Enumeration type is returned for the provided string, which must be exactly matched.Source codeStatement.
Method
One of our favorite aspects about enumeration types is that it can have methods. In the past, you may need to write some code to convert public static final int and convert it from database type to jdbc url. Now, we can add a code sorting method to the enumeration type. The following is an example, which includes an abstract method of the databasetype Enumeration type and the implementation provided in each enumeration instance:
Public Enum databasetype {

Oracle {

Public String getjdbcurl (){...}

},

MySQL {

Public String getjdbcurl (){...}

};

Public abstract string getjdbcurl ();

}

The enumerated type can provide its practical method directly. For example:

Databasetype dbtype = ...;
String jdbcurl = dbtype. getjdbcurl ();

To obtain a URL, you must know in advance where the practical method is.

 

Variable Parameter (vararg)
Correct use of variable parameters can indeed clear some junk code. A typical example is a log method with variable number of string parameters:
Log. Log (string code)

Log. Log (string code, string Arg)

Log. Log (string code, string arg1, string arg2)

Log. Log (string code, string [] ARGs)

When talking about variable parameters, it is interesting that if you replace the first four examples with the new variable parameters, they will be compatible:
Log. Log (string code, string... ARGs)
All variable parameters are source compatible-that is, if you recompile all calls to the log () methodProgram, You can directly replace all the four methods. However, if you need backward binary compatibility, you need to remove the first three methods. Only the method with the last string array parameter is equivalent to the variable parameter version, so it can be replaced by the variable parameter version.

Type forced conversion
If you want the caller to know which type of parameters should be used, you should avoid forced type conversion with variable parameters. Let's take a look at the example below. The first item is string, and the second item is exception:
Log. Log (object... objects ){

String message = (string) objects [0];

If (objects. length> 1 ){

Exception E = (exception) objects [1];

// Do something with the exception

}

}

The method signature should be as follows, and the variable parameters should be declared using string and exception respectively:

Log. Log (string message, exception E, object... objects ){...}

Do not use variable parameters to destroy the type system. It can be used only when it is strongly typed. For this rule, printstream. printf () is an interesting exception: it provides type information as its first parameter so that it can accept those types later.

Covariant return
The basic usage of covariant return is to avoid forced type conversion when the returned type of an implementation is known to be more specific than the API. In the following example, there is a zoo interface that returns the animal object. Our implementation returns an animalimpl object, but before JDK 1.5, an animal object must be declared. :
Public interface zoo {

Public animal getanimal ();

}

Public class zooimpl implements zoo {

Public animal getanimal (){

Return new animalimpl ();

}

}

The following three anti-modes are replaced in the returned result of the covariant:

 

Direct field access. To avoid API restrictions, some implementations expose subclass directly as fields:

Zooimpl. _ animal

Another form is to execute a downward conversion in the calling program when you know that the implementation is actually a specific subclass:

(Animalimpl) zooimpl. getanimal (). implmethod ();

The last form I see is a specific method, which is used to avoid problems caused by a completely different signature:

Zooimpl. _ getanimal ();

These three models have their own problems and limitations. Either it is not neat enough or it exposes unnecessary implementation details.

Covariant
The covariant return mode is neat, secure, and easy to maintain, and does not require type forced conversion or specific methods or fields:
Public animalimpl getanimal (){
Return new animalimpl ();
}
Result:
Zooimpl. getanimal (). implmethod ();

Use generic
We will understand generics from two perspectives: Using Generics and constructing generics. We will not discuss the obvious usage of list, set, and map. It is enough to know that the generic set is powerful and should be used frequently.
We will discuss how to use generic methods and how compilers deduce types. Generally, there will be no problems, but when there is a problem, the error information will be very confusing, so you need to know how to fix these problems.

Generic Method
In addition to generic types, Java 5 also introduces generic methods. In this example from Java. util. Collections, a single element list is constructed. The element type of the new list is inferred based on the object type of the input method:
Static <t> List <t> collections. singletonlist (t o)

Example usage:

Public list <integer> getlistofone (){

Return collections. singletonlist (1 );

}

In the example, an int is input. Therefore, the return type of the method is list <integer>. The compiler infers t to integer. This is different from the generic type because you do not need to explicitly specify the type parameter.
This also shows the interaction between automatic packing and generics. The type parameter must be of the Reference Type: this is why we get the list <integer> instead of the List <int>.

Generic method without Parameters
The emptylist () method is introduced with generics as a safe replacement of the empty_list field type in Java. util. Collections:
Static <t> List <t> collections. emptylist ()

Example usage:

Public list <integer> getnointegers (){

Return collections. emptylist ();

}

Unlike the previous example, this method has no parameters. How does the compiler infer the T type? Basically, it will try to use the parameter once. If it does not work, it tries to use the return or value type again. In this example, list <integer> is returned, so t is inferred as integer.
What if the generic method is called outside the return statement or value assignment statement? Then the compiler will not be able to execute the second transfer of type inference. In the following example, emptylist () is called from the condition OPERATOR:
Public list <integer> getnointegers (){

Return X? Collections. emptylist (): NULL;

}

Because the compiler does not see the returned context and cannot infer t, it abandons and uses the object. You will see an error message, such as: "list <Object> cannot be converted to list <integer> ."
To fix this error, type parameters should be passed explicitly to the method call. In this way, the compiler will not try to deduce the type parameter, so it can get the correct result:
Return X? Collections. <integer> emptylist (): NULL;

Another common cause of this situation is in method calling. If a method carries a list <string> parameter and needs to call the passed emptylist () parameter for that parameter, you also need to use this syntax.

Outside the set
There are three examples of generic types. They are not a set, but they are used in a novel way. All three examples come from the standard Java Library:

Class <t>
The class is parameterized on the class type. This makes it possible to construct a newinstance without forced type conversion.
Comparable <t>
Comparable is parameterized by the actual comparison type. This provides stronger typing for compareto () calls. For example, string implements comparable <string>. Calling compareto () for anything except string will fail during compilation.
Enum <E extends Enum <E>
Enum is parameterized by Enumeration type. Enum <color> is extended for an enumeration type named color. The getdeclaringclass () method returns class objects of the enumeration type. In this example, it is a color object. Unlike getclass (), the latter may return an unknown class.

Wildcard
The most complex part of generics is the understanding of wildcards. We will discuss three types of wildcards and their usage.
First, let's take a look at how arrays work. You can assign a value to a number [] from an integer. If you try to write a float to number [], you can compile it, but it will fail during runtime, and an arraystoreexception will appear:
Integer [] IA = new integer [5];

Number [] NA = IA;

Na [0] = 0.5; // compiles, but fails at runtime

If you try to convert this example to a generic type, the compilation will fail, because the assignment is not allowed:

List <integer> ilist = new arraylist <integer> ();

List <number> NLIST = ilist; // not allowed

NLIST. Add (1, 0.5 );

If you use generics, as long as the Code does not have a warning during compilation, you will not encounter classcastexception at runtime.

Upper Limit wildcard
What we want is a list of unknown exact element types, which is different from the array.
List <number> is a list whose element type is number.
List <? Extends number> is a list of unknown element types. It is a number or its subtype.

 

Upper Limit
If we update the initial example and assign it to list <? Extends number>:
List <integer> ilist = new arraylist <integer> ();

List <? Extends number> NLIST = ilist;

Number N = NLIST. Get (0 );

NLIST. Add (0.5); // not allowed

We can get number from the list, because no matter what the exact element type of the List is (float, integer, or number), we can assign it to number.
We still cannot insert the floating point type into the list. This will fail during compilation, because we cannot prove that this is safe. If we want to add a floating point type to the list, it will destroy the initial type security of ilist -- it only stores integer.
Wildcards give us more expressive power than arrays.

Why do I use wildcards?
In the following example, wildcards are used to hide type information from API users. Internally, set is stored as customerimpl. API users only know that they are acquiring a set from which they can read the customer.
Wildcards are required because they cannot be assigned from set <customerimpl> to set <customer>:
Public class customerfactory {

Private set <customerimpl> _ MERs MERS;

Public set <? Extends customer> getcustomers (){

Return _ MERs MERS;

}

}

Wildcard and coordinated return
Another common usage of wildcard is to use it together with the coordinated return. Rules with the same value assignment can be applied to the covariant return. If you want to return a more specific generic type in the override method, the declared method must use wildcards:
Public interface numbergenerator {

Public list <? Extends number> Generate ();

}

Public class fibonaccigenerator extends numbergenerator {

Public list <integer> Generate (){

...

}

}

If you want to use an array, the interface can return number [], while the implementation can return integer [].

Lower limit
We are talking about the upper limit wildcard. There is also a lower limit wildcard. List <? Super number> is a list that is unknown for the exact "element type", but may be mnumber or a super-type of number. Therefore, it may be a list <number> or a list <Object>.
Lower-limit Wildcards are far less common than upper-limit wildcards, but they are required when they are needed.

Lower limit and Upper Limit
List <? Extends number> readlist = new arraylist <integer> ();

Number N = readlist. Get (0 );

 

List <? Super number> writelist = new arraylist <Object> ();

Writelist. Add (New INTEGER (5 ));

The first one is the list from which reading can be made.
The second is a list of numbers that can be written.

Unbounded wildcard
Finally, list <?> The content of the list can be of any type, and it corresponds to the list <? Extends Object> is almost the same. You can read objects at any time, but cannot write content to the list.

Wildcards in public APIs
In short, as mentioned above, wildcards are very important in hiding implementation details from the caller, but even if the lower-limit wildcard seems to provide read-only access, since remove (INT position) and other non-generic methods. If you want a truly unchanged set, you can use methods on java. util. Collection, such as unmodifiablelist ().
Remember the wildcard when writing an API. In general, you should try to use wildcards when passing generic types. It allows more calling programs to access APIs.
By receiving list <? Extends number> instead of list <number>, the following method can be called by many different types of lists:
Void removenegatives (list <? Extends number> list );

Construct generic types
Now we will discuss how to construct your own generic type. We will show some examples, in which the use of generics can improve the type security, we will also discuss some common problems when implementing generic types.

Collection-like functions
The first example of a generic class is a collection style example. Pair has two types of parameters, and the fields are type instances:
Public final class pair <A, B> {

Public final a first;

Public final B second;

 

Public pair (a first, B Second ){

This. First = first;

This. Second = second;

}

}

This makes it possible to return two items from the method without writing a dedicated class for each combination of the two types. Another method is to return object [], which is insecure or untidy.
In the following usage, a file and a Boolean are returned from the method. The client of the method can directly use fields without forced type conversion:
Public pair <file, Boolean> getfileandwritestatus (string path ){

// Create File and status

Return new pair <file, Boolean> (file, status );

}

 

Pair <file, Boolean> result = getfileandwritestatus ("...");

File F = result. first;

Boolean writeable = result. Second;

Outside the set
In the following example, generics are used for additional compilation security. By setting the dbfactory class parameter to the created peer type, you actually force the factory subclass to return a specific child type of the Peer:
Public abstract class dbfactory <t extends dbpeer> {

Protected abstract t createemptypeer ();

Public list <t> get (string constraint ){

List <t> peers = new arraylist <t> ();

// Database magic

Return peers;

}

}

By implementing dbfactory <customer>, customerfactory must return a customer from createemptypeer:

Public class customerfactory extends dbfactory <customer> {

 

Public customer createemptypeer (){

Return new customer ();

}

}

Generic Method
Whether you want to impose constraints on generic types between parameters or between parameters and return types, you can use the generic method:
For example, if a reverse function is written to reverse the position, you may not need a generic method. However, if you want to reversely return a new list, you may want the element type of the new list to be the same as that of the imported list. In this case, a generic method is required:

 

<T> List <t> reverse (list <t> List)

Concrete
When implementing a generic class, you may want to construct an array T []. This is not allowed because generics are implemented through erasure.
You can try to forcibly convert object [] to T []. But this is not safe.

Specific Solution
According to the general practice of the generic tutorial, the solution uses a "type token". By adding a class <t> parameter to the constructor, you can force the client to provide the correct class object for class type parameters:
Public class arrayexample <t> {

Private class <t> clazz;

 

Public arrayexample (class <t> clazz ){

This. clazz = clazz;

}

 

Public T [] getarray (INT size ){

Return (T []) array. newinstance (clazz, size );

}

}

To construct arrayexample <string>, the client must pass string. class to the constructor, because the string. class type is class <string>.
Having class objects makes it possible to construct an array with the correct element type.
From: http://hi.baidu.com/edg_edu/blog/item/70c23b83caa4e1b46d811941.html
New Features of jdk1.7:
1. You can use strings in the switch.
String S = "test ";
Switch (s ){
Case "test ":
System. Out. println ("test ");
Case "test1 ":
System. Out. println ("test1 ");
Break;
Default:
System. Out. println ("break ");
Break;
}

 

From http://www.2cto.com/kf/201109/105891.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.