Four keywords of Java: transient, strictfp, volatile, final

Source: Internet
Author: User

I. Transient

Intended audience: Properties

Fields are not part of the object's persistent State and should not be concatenated with the object. When an object is serialized, the value of the transient variable is not included in the serialized representation, but not the transient variable is included. When an object is serialized, if a variable of the object is transient, the variable will not be serialized. That is to say, if the member variable of a class is transient, the value of the transient variable will not be saved when an instance of this class is saved to the disk through objectoutputstream.

Ii. strictfp

Objects used: classes and Methods

Since Java2, the Java language has added a keyword strictfp. Although this keyword is rarely used in most cases, it is necessary to understand it.

Strictfp refers to FP-strict, that is, exact 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 on the floating-point expressions in an action similar to the self-processing, 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 strictfp keyword because the results of different hardware platforms are inconsistent.

You can declare a class, interface, and method as strictfp, but you cannot declare the strictfp keyword for methods and constructors in the interface. For example, the following code:

1. Valid keyword strictfp

Strictfp InterfaceA {}

PublicStrictfpClass fpdemo1 {
StrictfpVoid F (){}
}

2. Incorrect usage

Interface {
StrictfpVoid F ();
}

Public class fpdemo2 {
StrictfpFpdemo2 (){}
}

Once the keyword strictfp is used to declare a class, interface, or method, all floating point operations within the scope declared by this keyword are accurate and comply with the IEEE-754 specification. For example, if a class is declared as strictfp, all the methods in this class are strictfp.

Iii. Volatile

Intended audience: Properties

Introduction: Because the asynchronous thread can access fields, some optimization operations cannot be applied to fields. Volatile sometimes

Synchronized can be replaced.

Iv. Final

The Java compiler optimizes final variables in Java. The final type variables are not accessed by connection. For example, if the test class uses a final int number fnumber = 77 in the data class, the Java compiler will compile the constant 77 to the script code or constant pool of the test class. In this way, each time the test class uses fnumber, it will not connect to the data class through reference for reading, but directly use its own copy saved in the class file.

Speak with a program:

C:/> javac test. javatest. Java:
Public class test {
Public static void main (string [] ARGs ){
System. Out. println (data. fnumber );
}
}

Data. Java:

Public class data {
Public static final int fnumber = 77;

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.