Java Lookup (1)

Source: Internet
Author: User

Transient keyword

Java has a feature of serialization. In simple terms, this class can be stored in physical space (of course, it still exists in the form of files). When you restore this file locally, you can convert it to itself. This can greatly facilitate some operations on the network, but at the same time, because of security issues, we do not want to store everything in the class (because of that, others can know the content in the class through serialization), so we can use the transient keyword, which means it is temporary, that is, it will not be serialized to the local along with the class, therefore, after restoration, the variables defined by this keyword will no longer exist.

For example, in the implementation of the file class in the Java D:/jdk1.5.0/src/Java/IO source package, define the prefixlength variable as a temporary variable:

/**
* The length of this abstract pathname's prefix, or zero if it has no
* Prefix.
*/
Private transient int prefixlength;

 

We can see the dataoutputstream implementation in the D:/jdk1.5.0/src/Java/IO source code package:

Public final void writeshort (INT v) throws ioexception {
Out. write (v >>> 8) & 0xFF );
Out. write (v >>> 0) & 0xFF );
IncCount (2 );
}

Public final void writeInt (int v) throws IOException {
Out. write (v >>> 24) & 0xFF );
Out. write (v >>> 16) & 0xFF );
Out. write (v >>> 8) & 0xFF );
Out. write (v >>> 0) & 0xFF );
IncCount (4 );
}

Public final void writeLong (long v) throws IOException {
WriteBuffer [0] = (byte) (v >>> 56 );
WriteBuffer [1] = (byte) (v >>> 48 );
WriteBuffer [2] = (byte) (v >>> 40 );
WriteBuffer [3] = (byte) (v >>> 32 );
WriteBuffer [4] = (byte) (v >>> 24 );
WriteBuffer [5] = (byte) (v >>> 16 );
WriteBuffer [6] = (byte) (v >>> 8 );
WriteBuffer [7] = (byte) (v >>> 0 );
Out. write (writeBuffer, 0, 8 );
IncCount (8 );
}

Public final void writeChar (int v) throws IOException {
Out. write (v >>> 8) & 0xFF );
Out. write (v >>> 0) & 0xFF );
IncCount (2 );
}

As you can see, the writeChar () method of this class occupies two bytes, Because Java is Unicode. If you are communicating with the java server, there will be no problems. However, if you are communicating with the C/C ++ server, problems may occur. In addition, the long type in this class is written into 8 bytes. Therefore, if your server is not written in java, it is best not to call methods in this class for communication.

 

Trackback: http://tb.blog.csdn.net/TrackBack.aspx? Postid = 1665683

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.