关于JDK中的transient关键字

  • 前段时间参加面试被问到了 HashMap 的原理,但是对 HashMap 的了解都是通过博客,一面建议我还是多看看源码,博客的知识毕竟有一些过时了,所以今天就来研究研究源码,然后发现HashMap的变量里有一个transient关键字,从来没见过,于是去百度了一下,现在做个总结好了~
Java's serialization provides an elegant, and easy to use mechanism for making an object's state persistent.

While controlling object serialization, we might have a particular object data member that we do not want the serialization

mechanism to save.To turn off serialization on a certain field of an object, we tag that field of the class of our object with

the Java's "transient" keyword. This, to low-level parts of the Java virtual machine, is an indication that the transient variable

is not part of the persistent state of an object.
  • 其实这个关键字说白了就是当对象需要序列化输出的时候,用户不希望某个变量被序列化输出,就用此关键字标记一下,然后当对象序列化输出时,被此关键字标记的变量是不会保存在硬盘中的,所以读取的时候该变量为空。
  • 参考文献

你可能感兴趣的:(Java)