Uncommon Keyword:strictfp,transient in Java

Source: Internet
Author: User

1.STRICTFP, which is the strict float point (exact float).
STRICTFP keyword can be applied to classes, interfaces, or methods. When declaring a method with STRICTFP keyword, all of the float and double expressions in the method adhere strictly to the fp-strict limit and conform to the IEEE-754 specification. When using STRICTFP keyword on a class or interface, all the code in the class, including the initial setpoint and code in the nested type, is evaluated Strictly. Strict constraints mean that the results of all expressions must be the result of an IEEE 754 algorithm's expected operand, expressed in single-precision and double-precision formats.
Suppose you want to make your floating-point arithmetic more precise and not be able to use KEYWORDSTRICTFP because of inconsistent results from different hardware platforms.
Demo Sample 1
The following demo sample demonstrates a class declared with the STRICTFP modifier.
Java code
Package com.magical;

Example of precision control with STRICTFP
Public STRICTFP class MyClass {
public static void Main (string[] args)
{
float afloat = 0.6710339f;
Double adouble = 0.04150553411984792d;
Double sum = afloat + adouble;
Float quotient = (float) (afloat/adouble);
System.out.println ("float:" + afloat);
System.out.println ("Double:" + adouble);
System.out.println ("sum:" + sum);
System.out.println ("quotient:" + quotient);
}
}

Package com.magical;

Example of precision control with STRICTFP
Public STRICTFP class MyClass {
public static void Main (string[] args)
{
float afloat = 0.6710339f;
Double adouble = 0.04150553411984792d;
Double sum = afloat + adouble;
Float quotient = (float) (afloat/adouble);
System.out.println ("float:" + afloat);
System.out.println ("Double:" + adouble);
System.out.println ("sum:" + sum);
System.out.println ("quotient:" + quotient);
}
}


Execution Result:
float:0.6710339
double:0.04150553411984792
sum:0.7125394529774224
quotient:16.167336

2.transient

When serializing an object, if a variable of that object is transient, then the variable is not serialized in. That is, if the member variable of a class is transient, then the

ObjectOutputStream an instance of this class

When saved to disk, the value of the transient variable is actually not saved. Since this object is read from disk, the object's variable will not be assigned a value.

In addition, this article mentions that when an instance of a class is read from disk, it does not actually run the constructor of the class, but instead reads the state of the instance of the class and pays the state to the object of the class.

Import java.util.*;
public class LoggingInfo implements Java.io.Serializable

{

Private date Loggingdate = new Date ();

Private String uid;

private transient String pwd;

LoggingInfo (string user, string password)

{

UID = user;

PWD = password;

}

Public String toString ()

{

String Password=null;

if (pwd = = null)

{

Password = "Not SET";

}

Else

{

Password = pwd;

}

Return "Logon info: \ n" + "User:" + UID +

"\ Logging Date:" + loggingdate.tostring () +

"\ n Password:" + password;

}

}

Import java.io.*;
public class serializable{


public static void Main (String args[]) {




LoggingInfo loginfo = new LoggingInfo ("Xiao Xu", "Don't Know");

System.out.println (Loginfo.tostring ());

Try

{

ObjectOutputStream o = new ObjectOutputStream (

New FileOutputStream ("Loginfo.out"));

O.writeobject (Loginfo);

O.close ();

}

catch (Exception e) {//deal with Exception

E.printstacktrace ();
}

To read the object back, we can write

Try

{

ObjectInputStream in =new ObjectInputStream (

New FileInputStream ("Loginfo.out"));

LoggingInfo logInfo1 = (logginginfo) in.readobject ();

System.out.println (Loginfo1.tostring ());

}

catch (Exception e)
{//deal with exception
E.printstacktrace ();
}

}
}

Import java.util.*;
public class Logginginfo_ implements Java.io.Serializable

{

Private date Loggingdate = new Date ();

Private String uid;

private transient String pwd;

Public Logginginfo_ ()

{

This.uid = "Xiao Xu";

This.pwd = "do not know";

}

Public String toString ()

{

String Password=null;

if (pwd = = null)

{

Password = "Not SET";

}

Else

{

Password = pwd;

}

Return "Logon info: \ n" + "User:" + UID +

"\ Logging Date:" + loggingdate.tostring () +

"\ n Password:" + password;

}

}

Import java.io.*;
public class serializable_{


public static void Main (String args[]) {


Logginginfo_ loginfo_ = new Logginginfo_ ();

System.out.println (Loginfo_.tostring ());

Try

{

ObjectOutputStream o = new ObjectOutputStream (

New FileOutputStream ("Loginfo_.out"));

O.writeobject (Loginfo_);

O.close ();

}

catch (Exception e) {//deal with Exception

E.printstacktrace ();
}

To read the object back, we can write

Try

{

ObjectInputStream in =new ObjectInputStream (

New FileInputStream ("Loginfo_.out"));

Logginginfo_ loginfo_1 = (logginginfo_) in.readobject ();

System.out.println (Loginfo_1.tostring ());

}

catch (Exception e)
{//deal with exception
E.printstacktrace ();
}
}

}

Uncommon Keyword:strictfp,transient 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.