Transient,volatile and STRICTFP keywords in Java __java

Source: Internet
Author: User
Tags volatile

Transient,volatile and STRICTFP keywords in Java

Reprinted from: http://www.iteye.com/topic/52957 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 class T {transient int A;  No need to maintain int b; Need to maintain}
Here, if an object of the T class is written to a persistent storage area, the content of a is not saved, but B's will be saved.
The volatile modifier tells the compiler that the variable modified by the volatile can be changed by other parts of the program. In multithreaded programs, there are times when two or more threads share an identical instance variable. Considering efficiency issues, each thread can save its own private copy of the shared variable. The actual variable copies are updated at different times, such as when entering the Synchronized method.
Modifying a class or method with STRICTFP ensures that floating-point operations (and all of them are cut off) are as accurate as earlier versions of Java. Cut off the exponent that affects only certain operations. When a class is strictfp decorated, all methods are automatically modified by STRICTFP.
STRICTFP means fp-strict, which means precise floating-point meaning. When a Java virtual machine does floating-point operations, if the STRICTFP keyword is not specified, the Java compiler and the runtime's expression for floating-point operations perform these operations in a way that is approximate to their own behavior, so that the results are often unsatisfactory. Once the STRICTFP is used to declare a class, interface, or method, the declared Java compiler and the runtime environment are executed entirely in accordance with the floating-point specification IEEE-754. So if you want to make your floating-point operations more accurate and not inconsistent with the results of different hardware platforms, use the keyword STRICTFP.
You can declare a class, interface, and method as STRICTFP, but do not allow the methods and constructors in the interface to declare STRICTFP keywords, such as the following code:

1. Legal use of keyword STRICTFP
Java code STRICTFP interface A {} public strictfp class FpDemo1 {strictfp void F ()}}
2. How to use the error
Java code interface A {STRICTFP void F (); public class FpDemo2 {STRICTFP FpDemo2 ()}}
Once a keyword STRICTFP is used to declare a class, interface, or method, all floating-point operations in the scope declared by the keyword are accurate and conform to the IEEE-754 specification. For example, if a class is declared as STRICTFP, then all the methods in the class are STRICTFP.



Keys:volatile

Working with objects: Fields

Introduction: Because Asynchronous threads can access fields, some optimizations do not work on fields. Volatile sometimes

can replace synchronized.




Keys:transient

Working with objects: Fields

Description: A field is not part of an object's persistent state and should not be strung together with the object.

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.