JAVA transient 关键字的用途

http://www.javabeat.net/tips/168-what-is-transient-keyword-in-java.html

 

What is transient keyword in Java?

Author : Sutha
Date : Wed Feb 18th, 2009
Topic : java

Translated by: iamjemy@gmail.com 

 

What is transient keyword in Java?

java中的关键字transient是干什么的?

What is Serilization? 

什么是序列化?

If you want to understand what is transient, then first learn what is serilization concept in Java if you are not familiar with that. Serilization is the process of making the object's state persistent. That means the state of the object is converted into stream of bytes and stored in a file. In the same way we can use the de-serilization concept to bring back the object's state from bytes. This is one of the important concept in Java programming because thisserilization is mostly used in the networking programming. The object's which are needs to be transmitted throughnetwork has to be converted as bytes, for that purpose ever class or interface must implements Serilizationinterface. It is a marker interface without any methods.

如果你想知道transient是什么,那么你必须首先了解什么是序列化。序列化是对象状态持久化的过程。这意味着对象的状态被转化为字节流并且保存在文件中。同样我们用反序列化的概念来从字节流中恢复对象。这是java编程中的重要概念因为序列化大量运用于网络编程中。需要通过网络传输的对象必需要进行序列化转换为字节流之后才能传输。因此所对应的类和接口必须实现Serilization接口。这个接口没有任何方法,只是一个标记接口。

What is Transient?

什么是瞬态?

By default all the variables in the object is converted into the persistent. In some cases, you may want to avoid persisting some variables because you don't have the necesscity to persist those varibale. So, you can declare those variables as transient. if the variable is declared as transient, then it will not be persisted. It is the main purpose of the transient keyword.

默认情况下,对象的所有成员变量都将被持久化。在某些情况下,如果你想避免持久化对象的一些成员变量,你可以使用transient关键字来标记他们。如果一个成员变量被标记为transient,那么它不会被持久化。这是transient关键字的主要用途。

Transient Keyword Example

Look into the following example to understand the purpose of transient keyword:


package javabeat.samples;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

class NameStore implements Serializable{
	private String firstName;
	private transient String middleName;
	private String lastName;
	public NameStore (String fName, String mName, String lName){
		this.firstName = fName;
		this.middleName = mName;
		this.lastName = lName;
	}
	public String toString(){
		StringBuffer sb = new StringBuffer(40);
		sb.append("First Name : ");
		sb.append(this.firstName);
		sb.append("Middle Name : ");
		sb.append(this.middleName);
		sb.append("Last Name : ");
		sb.append(this.lastName);
		return sb.toString();
	}
}
public class TransientExample{
	public static void main(String args[]) throws Exception {
		NameStore nameStore = new NameStore("Steve","Middle","Jobs");
		ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream(
				"nameStore"));
		// writing to object
		o.writeObject(nameStore);
		o.close();
		
		// reading from object
		ObjectInputStream in =new ObjectInputStream( 
                new FileInputStream("nameStore")); 
		NameStore nameStore1 = (NameStore)in.readObject();
		System.out.println(nameStore1);
	}
}

// output will be : First Name : SteveMiddle Name : nullLast Name : Jobs

In the above example, the variable middleName is declared as transient, so it will not be stored in the persistent storage. You can run the above example and check the results. If you have any doubts please click on the feedback button and send your queries. We will reply back ASAP.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值