New Features of Java 7

Source: Internet
Author: User
① The switch function supports strings. That is to say, you can directly use strings after the switch function. The syntax is basically the same as that supported before Java 7. ② Enhanced support for numeric literal volume. First, binary data can be directly used in the source code, but "0b" or "0b" must be added before the binary value, the second is to use underscores (_) in the literal values to facilitate reading, but they are not allowed before or after a number, for example, "123 _" or "_ 123". In other words, the underline can only appear in the middle of the number; otherwise, it cannot be compiled. ③ Multiple exceptions can be caught in a catch clause at the same time, and the exception type can be more precise when an exception is caught and thrown again. Try {Int J = 4/0;} catch (arithmeticexception | nullpointerexception e) {} finally {system. out. println ("no problem");} Use "|" to separate each exception type, but note that in the catch clause, declare the caught exception classes, duplicate types are not allowed, or an exception is a subclass of another exception. Otherwise, the following compilation error occurs: "error: substitution in Multi-catch statements cannot be correlated through subclass ". If multiple exception classes are declared in the catch clause, the specific type of the exception parameter is the minimum upper bound of all these exception types. ④ When the try-with-resources statement is used, Java 7 enhances the try-with-resources statement so that it supports automatic resource recovery management to ensure that resources are always correctly released. Note: resources that can be managed by the try statement must meet one condition, that is, the Java class must implement Java. lang. autocloseable interface. Otherwise, a compilation error occurs. When resources need to be released, the close method of this interface is automatically called. ⑤ Java 7 introduced a new annotation, @ safevarargs, which means that if a developer is sure that a variable length parameter is used, this annotation can be used for declaration without any type security issues when used together with generic classes. After this annotation is used, the compiler will not give any warning information when it encounters a similar situation. @ Safevarargs annotation can only be used in methods or constructor with variable parameter length, and the method must be declared as static or final. Otherwise, a compilation error occurs. The premise for a method to use @ safevarargs annotation is that developers must ensure that the processing of generic parameters in the implementation of this method does not cause type security problems. ⑥ Script Engine Introduction: the execution of a script must be completed by the script engine corresponding to the script language. A Java program can select an execution engine that contains multiple scripting languages at the same time, which is entirely determined by the needs of the program. The script engine is required for all the scripting languages used in the program. The script engine Manager supports three methods to search for the script engine, including name, file extension, and MIME type. Language binding: A language binding object is a simple hash table used to store and obtain data to be shared. All data corresponds to an entry in the hash table and is a simple name-value pair. Script Compilation: the script language is generally interpreted and executed. The script engine needs to parse the script before executing it. Generally, it is slower to run the script by interpreting the execution method than to run the script after compilation. When a script needs to be executed multiple times, you can compile the script first. The compiled script does not need to be parsed repeatedly during execution, which improves the execution efficiency. Not all script engines Support compiling scripts. If the Script Engine supports this feature, it implements the javax. Script. compilable interface to declare this. Users of the script engine can use this capability to improve the running efficiency of scripts that require multiple executions. Java
The Javascript script engine in SE supports compiling scripts. 7. If the constructor declares a variable-length parameter, class objects of the corresponding array type should be used when obtaining the constructor. This is because variable-length parameters are actually implemented through arrays. Static internal class calls are not special. You can use them in general mode. For non-static internal classes, the special feature is that there is an implicit object reference in its object instance, pointing to the external class objects that contain it. It is also the existence of this implicit object reference, so that the code in the non-static internal class can directly reference the private domain and method contained in the external class. Therefore, when obtaining the constructor of a non-static internal class, the first value of the type parameter list must be the class Object of the external class. It is worth mentioning that Java 7 adds a new parent class java. Lang. reflectiveoperationexception for all reflection-related exception classes. When handling reflection-related exceptions, you can directly catch this new exception. Before Java 7, these exceptions must be captured separately. A strong executable reference to methods, constructor methods, and fields in Java. The method handle can directly call the underlying method referenced by the handle. For a method handle, its type is completely determined by its parameter type and return value type, but it has nothing to do with the name of the underlying method referenced by it and the class it belongs. The method handle is represented by the java. Lang. Invoke. methodhandle class. After obtaining a method handle, you can view its type through its type method. The return value of this method is an object of the Java. Lang. Invoke. methodtype class. All object instances of the methodtype class are immutable, similar to the string class. All modifications to the methodtype class object will generate a new methodtype object. Whether two methodtype objects are equal depends on whether they contain the same parameter type and return value type.

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.