Java keyword: Introduction to TRANSIENT,STRICTFP and volatile

Source: Internet
Author: User

Keyword:transient

Working with objects: Fields

Description: Transient indicates that a property is temporary and will not be serialized.

When an object is serialized (Serializable) The state of some properties is instantaneous, such that an object cannot hold its state (such as a thread object, or a FileInputStream object), for these fields, We must use the transient keyword to indicate.

Adding a keyword transient to a property that does not require serialization, when serializing an object, does not serialize the property to the specified destination.

Keywords:volatile

Working with objects: Fields

Description:volatile allows variables to be taken from main memory each time they are used. Instead of "working memory" from each thread.

(used in multi-threaded, synchronous variables.) In order to improve efficiency, a thread may copy one copy (such as B) from a member variable (such as a), and the access to a in the thread actually accesses B. Synchronization of A and B occurs only in certain actions. Thus there is a and B inconsistency. Volatile is used to avoid this situation. Volatile tells the JVM that the variable it modifies does not retain the copy and accesses the main memory directly. )

Volatile has the "visibility" of the synchronized keyword, but there is no "concurrency correctness" of the synchronized keyword, which means that the ordering of thread execution is not guaranteed.

(that is, the volatile variable is the most recent value of the current volatile variable for each use.) However, volatile variables do not guarantee concurrency correctness. )

Keyword:strictfp

Working with objects: fields, classes

Introduction: Since Java2, the Java language has added a keyword STRICTFP, although this keyword is less useful in most cases, but it is necessary to understand.

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:

STRICTFP interface A {}

Public STRICTFP class FpDemo1 {strictfp void F () {}}

2. How to use the error:

Interface A {strictfp void F ();}

public class FpDemo2 {STRICTFP FpDemo2 () {}}

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.

Java keyword: Introduction to TRANSIENT,STRICTFP and volatile

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.