Transient,volatile and STRICTFP keywords in Java

Source: Internet
Author: User

If you declare an instance variable with transient, its value does not need to be maintained when the object is stored. For example:

Java code

    1. class T

    2. transient  int A; //Do not need to maintain

    3. int b; //need to maintain

    4. }


here, if an object of the T class is written to a persistent storage area, the contents of a are not saved, but B's will be saved.
The volatile modifier tells the compiler that variables modified by volatile can be changed by other parts of the program. In multithreaded programs, sometimes two or more threads share an identical instance variable. Consider the efficiency issue, where each thread can save its own private copy of the shared variable. The actual copy of the variable is updated at different times, such as when entering the Synchronized method.
using STRICTFP to decorate classes or methods ensures that floating-point operations (and all cuts) are as accurate as earlier versions of Java. Cuts the exponent that affects only certain operations. When a class is STRICTFP modified, all methods are automatically modified by STRICTFP.
strictfp means fp-strict, that is to say, the meaning of the precise floating point. When a Java virtual machine makes a floating-point operation, if the STRICTFP keyword is not specified, the Java compiler and the runtime's expression on floating-point operations do so in an approximate way, so that the resulting results do not always satisfy you. Once STRICTFP is used to declare a class, interface, or method, the compiler and the runtime environment for the declared scope of Java are executed exactly as the floating-point specification IEEE-754. So if you want your floating-point arithmetic to be more accurate and not inconsistent with the results of different hardware platforms, then use the keyword STRICTFP.
you can declare a class, interface, and method as STRICTFP, but do not allow the STRICTFP keyword to be declared on methods and constructors in the interface, such as the following code:

1. Legal use of keyword STRICTFP

Java code

    1. STRICTFP Interface A {}

    2. Public STRICTFP class FpDemo1 {

    3. STRICTFP void f () {}

    4. }


2. How to use the error

Java code

  1. Interface A

  2. STRICTFP void f ();

  3. }

  4. Public class FpDemo2 {

  5. STRICTFP FpDemo2 () {}

  6. }


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 conform to the IEEE-754 specification. For example, if a class is declared as STRICTFP, then all methods in the class are STRICTFP.



Keys:volatile

working with objects: Fields

Description: Because asynchronous threads can access fields, some optimizations do not necessarily work on fields. Volatile sometimes

can replace synchronized.




keys:transient

working with objects: Fields

Description: The field is not part of the object's persistent state, and the fields and objects should not be strung together.


Transient,volatile and STRICTFP keywords in Java

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.