Details the new features of Java5 Java6 and Java7

  • 2020-06-23 00:29:57
  • OfStack

Java5:

1. Generic Generics:

Referring to generics allows you to specify the type of elements in a collection without casting, and provides the benefit of type checking at compile time. As parameters and return values, Generic is the cornerstone of vararg, annotation, enumeration, collection.

A type safety

Discard List and Map, add elements to them using List and Map, or use Iterator traversal, and compile time will check you for type errors

B, method parameters, and return values are added with Type

List and Map were abandoned and List and Map were used

No type conversion is required

List list=new ArrayList();
String str=list.get(i);

D, type wildcard "?"

Assuming a method printList that prints elements in List, we want List of any type of T to be printed:

Code:


public void printList(List list,PrintStream out)throws IOException{
for(Iterator i=list.iterator();i.hasNext();){
System.out.println(i.next.toString());
}
}

What if wildcards? To make our parameter types too broad, we can change List and Iterator to

List, Iterator limit 1 it.

2. Enumeration type Enumeration:

3. Automatic packing and unpacking (automatic type packing and unpacking) autoboxing & unboxing:

Simply put, type automatic conversion.

Automatic package: The basic type is automatically converted to a wrapper class (int -- Integer)

Automatic unpacking: Packaging classes are automatically converted to basic types (Integer -- int)

4, variable parameter varargs(varargs number of arguments)

The same parameter type merges the overloaded function to 1.
Such as:


public void test(object... objs){
for(Object obj:objs){
System.out.println(obj);
}
}

It is the metadata of java

A, Tiger three predefined standards annotation

a, Override

Indicates that an method overrides superclass's method when the method name you are overwriting is misspelled

b, Deprecated

Indicates that the use of an method or element type is blocked and that subclasses will not be able to override the method

c, SupressWarnings

Turn off the compile time warnings initialized by class, method, field, variable. For example: List is not using Generic, @SuppressWarnings ("unchecked") removes the compile time warnings.

B, custom annotation

public @interface Marked{}

C, meta - annotation

Or annotation for annotation

The four standard ES178en-ES179en are all defined in the package ES180en.lang.ES182en:

a, Target

Specifies which program units the defined annotation can be used on

If Target is not specified, it means that annotation can be used on any program unit

code


 @Target({ElementType.ANNOTATION_TYPE, 
       ElementType.CONSTRUCTOR, 
       ElementType.FIELD, 
       ElementType.LOCAL_VARIABLE, 
       ElementType.METHOD, 
       ElementType.PACKAGE, 
       ElementType.PARAMETER, 
       ElementType.TYPE}) 
  public @interface TODO {} 

b, Retention

How does the Java compile time treat annotation

annotation can be dropped at compile time or kept in compiled class files

When annotation is retained, it also specifies whether annotation will be read when JVM loads class

code


 @Retention(RetentionPolicy.SOURCE) // Annotation Will be discarded at compile time  
  public @interface TODO1 {} 
  @Retention(RetentionPolicy.CLASS)  // Annotation Remain in the class File, but will be JVM ignore  
  public @interface TODO2 {} 
  @Retention(RetentionPolicy.RUNTIME) // Annotation Remain in the class File and will be JVM read  
  public @interface TODO3 {} 

c, Documented

Indicates that the defined annotation is considered to be public API 1 of a familiar program unit

annotation annotated with @Documented is displayed in javadoc, which is effective when annotation has an impact on how its annotated elements are used by clients

d, Inherited

This ES246en-ES247en is applied to the annotation type targeted at class, and class annotated by this annotattion automatically inherits annotation of the parent class

D, Annotation reflection

We found that java. lang. Class has many methods related to reflection of Annotation, such as getAnnotations, isAnnotationpresent

There are many things you can do with Annotation reflection, such as customizing Annotation to do Model object validation

code


 @Retention(RetentionPolicy.RUNTIME) 
  @Target({ ElementType.FIELD, ElementType.METHOD }) 
  public @interface RejectEmpty { 
    /** hint title used in error message */ 
    String value() default ""; 
  }    
  @Retention(RetentionPolicy.RUNTIME) 
  @Target( { ElementType.FIELD, ElementType.METHOD }) 
  public @interface AcceptInt { 
    int min() default Integer.MIN_VALUE; 
    int max() default Integer.MAX_VALUE; 
    String hint() default ""; 
  } 

Use @RejectEmpty and @ES281en to mark our Model's field and then use reflection to do Model validation

6. New iteration statement (for(int n:numbers))

7. Static import (import static)

8. New formatting method (ES300en. util. Formatter)

formatter.format("Remaining account balance: $%.2f", balance);

9. New thread model and concurrent library Thread Framework

ConcurrentHashMap and CopyOnWriteArrayList

java.util.concurrent package for large concurrent reads 1 class will make everyone happy BlockingQueue, Callable, Executor, Semaphore...

Java6:

1. A new framework supporting the script engine was introduced

2. Enhancement of UI

3. Enhancements to WebService support (JAX-WS2.0 and JAXB2.0)

4. 1 series of new security-related enhancements

5, JDBC4. 0

6, Compiler API

7. General Annotations support

Java7:

1. Strings are now available in 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 ; 
}

2. Use List tempList = new ArrayList < > (a); That is, generic instantiation types are automatically inferred

3. Syntax supports collections, not arrays

final List piDigits = [ 1,2,3,4,5,8 ];

4. Add 1 tool method to get environment information


File System.getJavaIoTempDir() // IO Temporary folder 
File System.getJavaHomeDir() // JRE Installation directory of 
File System.getUserHomeDir() //  Current user directory 
File System.getUserDir() //  Start the java The directory in which the process occurred 5

5.Boolean type inversion, null pointer safety, participate in bit operation


Boolean Booleans.negate(Boolean booleanObj)
True => False , False => True, Null => Null
boolean Booleans.and(boolean[] array)
boolean Booleans.or(boolean[] array)
boolean Booleans.xor(boolean[] array)
boolean Booleans.and(Boolean[] array)
boolean Booleans.or(Boolean[] array)
boolean Booleans.xor(Boolean[] array)

6. equals between two char

boolean Character.equalsIgnoreCase(char ch1, char ch2)

7. Safe addition, subtraction, multiplication and division


int Math.safeToInt(long value)
int Math.safeNegate(int value)
long Math.safeSubtract(long value1, int value2)
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)

8.map collection supports concurrent requests and can be written as Map map = {name:"xxx",age:18};

I hope this article can help those in need


Related articles: