简体   繁体   English

Java瞬态关键词语法糖?

[英]Is Java transient keyword syntactic sugar?

Why wasn't transient solved by using annotations instead of introducing a keyword? 为什么不通过使用注释而不是引入关键字来解决transient

It seems like the transient keyword doesn't really provide any native functionality to the language, and seems to be more syntactic-sugar to prevent accidental serialization. 似乎transient关键字并没有真正为语言提供任何本机功能,并且似乎更具有语法性,以防止意外序列化。

Am I missing something? 我错过了什么吗?

Java transient keyword was added from the first java version, while Annotations was added later in 1.5 version 从第一个java版本添加了Java transient关键字 ,而后来在1.5版本中添加了Annotations

Actually transient uses ad hoc annotation mechanism : 实际上,瞬态使用ad hoc注释机制

Java platform has always had various ad hoc annotation mechanisms. Java平台一直有各种ad hoc注释机制。 For example the transient modifier is an ad hoc annotation indicating that a field should be ignored by the serialization subsystem 例如,transient修饰符是ad hoc注释,指示序列化子系统应忽略该字段

Notice the Scala has @transient and also volatile: 请注意, Scala具有@transient且也是volatile:

@transient Marks a field to be non-persistent; @transient将字段标记为非持久性; this is equivalent to the transient modifier in Java. 这相当于Java中的transient修饰符。

@volatile Marks a field which can change its value outside the control of the program; @volatile标记一个可以在程序控制之外改变其值的字段; this is equivalent to the volatile modifier in Java. 这相当于Java中的volatile修饰符。

The transient keyword predates annotations as per the comment above. transient关键字根据上面的注释预先注释。 You could use AOP to create your own marker and hence annotate wherever you want to make class members transient although your markers would still be using the transient keyword in the background. 你可以使用AOP创建自己的标记,因此注释你想让类成员瞬态的任何地方,尽管你的标记仍然会在后台使用transient关键字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM