1. Strings can be added to the switch conditional statement. The method is to use the hashcode() value of the string to obtain the real value.
2. Added a base system that can be used in literals, binary, by adding "0b" or "0B" in front of the number.
3. Use underscores in numeric literals to separate numbers for easier reading without affecting the size of the value. The basic principle is that underlines can only appear if there are numbers before and after them.
4.java7 has made two changes to exceptions:
4.1. Supports catching multiple exceptions at the same time in a catch clause, and the other is to make the exception type more precise when catching and rethrowing exceptions. The addSuppressed method was added to the Throwable class in Java 7. When an exception is thrown, other exceptions may be suppressed because of the exception, and thus cannot be thrown normally. At this time, you can record these suppressed methods through the addSuppressed method. The suppressed exceptions will appear in the stack information of the thrown exceptions. You can also obtain these exceptions through the getSuppressed method. The advantage of this is that no exceptions will be lost, making it easier for developers to test.
Java7 improves the syntax of the catch clause, allowing multiple exceptions to be specified in it. Each exception type is separated by "|". It should be noted that the exceptions captured in the catch clause cannot have repeated types, nor is one of the exceptions allowed to be a subclass of another exception parameter, otherwise a compilation error will occur (from small to upper case, there is no problem ). If multiple exceptions are declared in the catch clause, the specific type of the exception parameter is the minimum upper bound of all these exception types.
4.2 Use try (resource application) {business processing} to automatically release resources. Resources that can be managed by try statements need to meet one condition, that is, their java class must implement the java.lang.AutoCloseable interface, otherwise a compilation error will occur. The close method of this interface will be automatically called when resources need to be released.
5. Optimize method calls with variable length parameters:
A new feature introduced in j2se5.0 is to allow the use of variable length parameters in method declarations. The last formal parameter of a method can be specified to represent any number of parameters of the same type. When called, these parameters are passed in the form of an array. These parameters can also be referenced as arrays in the method body.
6. Java 7 introduces a new annotation @SafeVarargs. If the developer is sure that a method that uses variable length parameters will not cause a similar situation when used with a generic class, it can be declared with this annotation. The @SafeVarargs annotation can only be used on methods or constructors with variable parameter lengths, and the method must be declared static or final, otherwise a compilation error will occur. The premise for a method to be annotated with @SafeVarargs is that the developer must ensure that the processing of generic type parameters in the implementation of this method does not cause type safety issues.
7.Java supports some scripting languages in the java virtual machine through the script engine. In fact, the script engine manager supports three search engine methods, which are completed by name, file extension and MIME type. like
7.1 Language binding:
A great advantage of scripting language support API is that it standardizes the interaction between Java language and scripting language, so that programs written in Java language can conduct two-way method calls and data transfers with scripts. Data transfer is done through language binding objects. The so-called language binding object is a simple hash table used to store and obtain data that needs to be shared. All data corresponds to an entry in this hash table and is a simple name-value pair. The interface javax.script.Bingings defines the interface of language binding objects, which inherits from the java.util.Map interface. A script engine may use multiple language binding objects during execution. Different languages have different scopes for binding objects. By default, the script engine will provide multiple language binding objects to store global objects generated during execution. The ScriptEnging class provides put and get methods to operate on the default language binding objects used specifically in the script engine. Programs can directly use this default language binding object, or they can use their own language binding objects. During script execution, the language binding object can be regarded as an additional variable mapping table. Names in language binding objects are also taken into account when parsing variable values. Global variables and other content generated during script execution will appear in the language binding object. In this way, the two-way data transfer between Java and scripting language is completed.
For example, a string named "name" is added to the script engine's default language binding object through the put method of ScriptEngine, and then the object is directly referenced by name in the script. Similarly, the global variable "message" created in the script can also be obtained through the get method of ScriptEnging. This achieves two-way data transfer between Java programs and scripts. Type conversion during data transfer is completed by the script engine, and the conversion rules depend on the grammar of the specific language.
In most cases, using ScriptEnging's put and get methods is sufficient. If only the put and get methods are used, the language binding object itself is transparent to the developer. In some cases, it is necessary to use the program's own language binding object. For example, the language binding object contains the program's own unique data. If you want to use your own language binding object, you can call the script engine's creatBingings method or create a javax.script.SimpleBingings object and pass it to the script engine's eval method, such as:
The language binding object passed through the eval method only takes effect in the current eval call and does not change the engine's default language binding object.
7.2 Script execution context Another important interface related to script engine execution is javax.script.ScriptContext, which contains relevant context information during the execution of the script engine. It can be compared with the javax.servlet.ServletContext interface in the servlet specification in JavaEE. . The script engine obtains information related to script execution by referencing the context object, and also allows developers to configure the behavior of the script engine through this object. The upper and lower objects mainly contain the following three types of information.
7.2.1 Input and Output First, the configuration information related to script input and output is introduced, including the java.io.Reader object used by the script to read data input during execution and the java.io.Writer that outputs correct content and error information. object. By default, the input and output of the script occur in the standard console. If you want to write the output of the script to a file, you can use the following code. Redirect the output of the script to a file through the setWriter method. Through the setReader and setErrorWriter methods of ScriptContext, you can respectively set the data input source when the script is executed and the output destination of the error message when an error occurs.
7.2.2 Custom properties
ScriptContext also has methods for getting and setting attributes similar to those in ServletContext, namely setAttribute and getAttribute. The difference is that the attributes in ScriptContext are scoped. The difference between different scopes is the search order. Each scope uses a corresponding integer to represent its search order. The smaller the integer value is, the higher the priority is in the search order. Properties in a scope with a higher priority will hide properties with the same name in a scope with a lower priority. Therefore, you need to explicitly specify the scope when setting properties. When obtaining attributes, you can choose to search in a specified scope, or you can choose to automatically search based on scope priority.
However, the scope included in the script execution context implementation is fixed. Developers are not free to define their own scope. A list of all available scopes can be obtained through the getScopes method of ScriptContext. There are two scopes predefined in SciptContext: the scope represented by the constant ScriptContext.ENGINE_SCOPE corresponds to the current script engine, and the scope represented by ScriptContext.GLOBAL_SCOPE corresponds to all script engine objects created from the same engine factory. The former has higher priority. For example:
7.2.3 Language binding objects
The final type of information in the script execution context is the language binding object. Language binding objects also correspond to scopes. The same order of scope precedence applies to language-bound objects. This priority order will have an impact on variable resolution during script execution. For example:
bindings.put("name","World")
engine.eval("println(name);");
7.3 Compilation of scripts:
Scripting languages are generally interpreted and executed. The script engine needs to parse the script before executing it at runtime. Generally speaking, running a script through interpretation will be slower than running it after compilation. When a script needs to be executed multiple times, the script can be compiled first. The compiled script does not need to be parsed repeatedly when executed, which can improve execution efficiency. Not all script engines support compilation of scripts. If a script engine supports this feature, it will implement the javax.script.Compilable interface to declare this. Users of script engines can take advantage of this capability to improve the efficiency of scripts that need to be executed multiple times. The JavaScript script engine that comes with Java SE supports compiling scripts.
In the following code, the compile method of the Compilable interface is used to compile the script code, and the compilation result is represented by javax.script.CompiledScript. Since not all script engines support the Compilable interface, instanceof needs to be used for judgment here. In the run method, the script can be executed through the eval method of CompiledScript. In the code, a script is executed repeatedly 100 times to illustrate the performance advantage of the compiled script when executed repeatedly.
public void run(String scriptText) throws ScriptException {
CompiledScript script = compile(scriptText);
if (script == null) {
return;
}
for (int i = 0; i < 100; i++) {
script.eval();
}
}
7.4 Method calls in scripts In scripts, the most common and practical ones are methods. Some script engines allow users to individually call a method in a script. Script engines that support this method invocation can implement the javax.script.Invocable interface. Through the Invocable interface, you can call top-level methods in scripts and member methods in objects. If the top-level method in the script or the member method in the object implements the interface in Java, you can obtain the implementation object of the corresponding Java interface in the script through the method in the Invocable interface. In this way, the interface can be defined in the Java language and implemented in the script. Other parts of the program that use the interface do not know that the interface is implemented by the script. Like the Compilable interface, ScriptEngine's implementation of the Invocable interface is also optional.
The following code calls the top-level method in the script through the invokeFunction of the Invocable interface. The parameters during the call will be passed to the method in the script. Because the JavaScript script engine that comes with JavaSE implements the Invocable interface, the judgment of whether the engine implements the Invocalbe interface is omitted here. An example of calling the script's top-level method in Java:
//Example of calling member methods of script object in Java
7.5 Implement java interface in script
In some script engines, the interface can be defined in the Java language and the implementation of the interface can be written in the script. In this way, other parts of the program can only interact with the Java interface and do not need to care about how the interface is implemented. In the following code, Greet is an interface defined in Java, which contains a getGreeting method. Implement this interface in the script. Through the getInterface method, you can get the object of the interface implemented by the script and call the methods in it.
Because the syntax of the script language is simple and flexible, it is very suitable for users with no or only a small amount of programming background. These users can use the script language to customize the business logic and user interface of the program. The script language can improve the ease of use of the program. A better balance is achieved between flexibility and flexibility. For example, the scripting language Lua is widely used in game development to customize the internal behavior and user interface of the game.
8. While the reflection API brings flexibility to Java programs, it also incurs additional performance costs. Due to the implementation mechanism of the reflection API, for the same operation, such as calling a method, using the reflection API to dynamically implement it is faster than directly executing it in the source. The way the code is written is probably one to two orders of magnitude slower. With the improvement of Java virtual machine implementation, the performance of reflection API has been greatly improved. However, this performance gap exists objectively. Therefore, in some applications that have relatively high performance requirements, reflection APIs should be used with caution.