1. Native
Native is a method modifier. The native method is a local method implemented by another language (such as C/C ++, Fortran, assembly. Because the method is implemented externallyCode. Native can be used with other modifiers, but abstract and interface methods cannot be modified using native.
Public Interface Testinterface {
VoidDomethod ();
}
Public Class Test implements testinterface {< br> Public native void domethod ();
private native int domethodb ();
Public native synchronized string domethodc ();
static native void domethodd ();
}
Why native method? See:
Http://www.javaeye.com/topic/72543Java Native Method
2. strictfp
modifier classes and Methods It indicates FP-strict, precise floating point. When the strictfp keyword is not specified for floating-point operations on the Java Virtual Machine, the Java compiler and runtime environment perform these operations in an action similar to the self-processing of floating-point expressions, as a result, you are often not satisfied with the results. Once strictfp is used to declare a class, interface or method, the declared scope of the Java compiler and runtime environment will be fully executed in accordance with the floating point specification IEEE-754. Therefore, if you want to make your floating point operations more accurate and do not use the keyword strictfp because the results of different hardware platforms are inconsistent .. When a class or interface is declared using strictfp, all internal float and double expressions will become strictfp.
Note: You can declare a class, interface, and method as strictfp, but you cannot declare strictfp keywords for methods and constructors in the interface.
StrictfpInterfaceFptest {
VoidMethoda ();
}
ClassFpclass implements fptest {
Public VoidMethoda () {
}
Public VoidMethodb () {
}
PublicStrictfpVoidMethodc () {
}
}
class fpclassb {
StrictfpVoidMethoda (){
}
}
3. Transient
Variable modifier (Only fields can be modified). Variables marked as transient are not persistent during object storage. When the object is serialized and stored in the memory, some field data is not expected to be saved. To ensure security, you can declare these fields as transient.
Code
Class Test {
Static Int I = 0 , J = 0 ;
Static Synchronized Void One () {I ++ ; J ++ ;}
Static Synchronized Void Two (){
System. Out . Println ( " I = " + I + " J = " + J );
}
}
4. Volatile
Volatile modifier variable. Every time a thread is accessed, the value of the member variable is forcibly re-read from the shared memory. In addition, when the member variables change, the thread is forced to write the change value back to the shared memory. In this way, two different threads always see the same value of a member variable at any time.
Let's look at the example in Java language specification.
Condition: A thread continuously calls Method One (), and a thread continuously calls method two (). I have tested it many times and it seems that this situation has never occurred.
Note: Because the asynchronous thread can access fields, some optimization operations must not work on fields. Volatile can sometimes replace synchronized
Class Test {
Static Int I = 0 , J = 0 ;
Static Void One () {I++; J++;}
Static Void Two () {
System.Out. Println ("I =" +I+ "J =" +J );
}
}
Occasionally, J is greater than I. Because the method is not synchronized, I and j may not be updated once. One way to prevent this is to declare two methods as synchronized.
Class Test {
Static Int I = 0 , J = 0 ;
Static Synchronized Void One () {I++; J++;}
Static Synchronized Void Two () {
System.Out. Println ("I =" +I+ "J =" +J );
}
}
This prevents two methods from being executed at the same time, and ensures that J and I are updated at the same time, so that the values of I and j are always the same.
Another way is to declare I and j as volatile.
Code
Class Test {
Static Volatile Int I = 0 , J = 0 ;
Static Void One () {I ++ ; J ++ ;}
Static Void Two (){
System. Out . Println ( " I = " + I + " J = " + J );
}
}