糯米文學吧

位置:首頁 > 計算機 > java語言

Java transient關鍵字使用總結

java語言1.34W

雖然小編自己最熟的是Java,但很多Java基礎知識都不知道,比如transient關鍵字以前都沒用到過,所以不知道它的作用是什麼,今天做筆試題時發現有一題是關於這個的,於是花個時間整理下transient關鍵字的使用,漲下姿勢~~~好了,廢話不多説,下面開始:

Java transient關鍵字使用總結

  1. transient的作用及使用方法

我們都知道一個對象只要實現了Serilizable接口,這個對象就可以被序列化,java的這種序列化模式為開發者提供了很多便利,我們可以不必關係具體序列化的過程,只要這個類實現了Serilizable接口,這個類的所有屬性和方法都會自動序列化。

然而在實際開發過程中,我們常常會遇到這樣的問題,這個類的有些屬性需要序列化,而其他屬性不需要被序列化,打個比方,如果一個用户有一些敏感信息(如密碼,銀行卡號等),為了安全起見,不希望在網絡操作(主要涉及到序列化操作,本地序列化緩存也適用)中被傳輸,這些信息對應的變量就可以加上transient關鍵字。換句話説,這個字段的生命週期僅存於調用者的內存中而不會寫到磁盤裏持久化。

總之,java 的sient關鍵字為我們提供了便利,你只需要實現Serilizable接口,將不需要序列化的屬性前添加關鍵字transient,序列化對象的時候,這個屬性就不會序列化到指定的目的地中。

示例code如下:

import InputStream;

import NotFoundException;

import OutputStream;

import ception;

import ctInputStream;

import ctOutputStream;

import alizable;

/**

* @description 使用transient關鍵字不序列化某個變量

* 注意讀取的時候,讀取數據的順序一定要和存放數據的順序保持一致

*

* @author Alexia

* @date 2013-10-15

*/

public class TransientTest {

public static void main(String[] args) {

User user = new User();

sername("Alexia");

asswd("123456");

tln("read before Serializable: ");

tln("username: " + sername());

tln("password: " + asswd());

try {

ObjectOutputStream os = new ObjectOutputStream(

new FileOutputStream("C:/"));

eObject(user); // 將User對象寫進文件

h();

e();

} catch (FileNotFoundException e) {

tStackTrace();

} catch (IOException e) {

tStackTrace();

}

try {

ObjectInputStream is = new ObjectInputStream(new FileInputStream(

"C:/"));

user = (User) Object(); // 從流中讀取User的數據

e();

tln("nread after Serializable: ");

tln("username: " + sername());

tln("password: " + asswd());

} catch (FileNotFoundException e) {

tStackTrace();

} catch (IOException e) {

tStackTrace();

} catch (ClassNotFoundException e) {

tStackTrace();

}

}

}

class User implements Serializable {

private static final long serialVersionUID = 8294180014912103005L;

private String username;

private transient String passwd;

public String getUsername() {

return username;

}

public void setUsername(String username) {

name = username;

}

public String getPasswd() {

return passwd;

}

public void setPasswd(String passwd) {

wd = passwd;

}

}

  輸出為:

read before Serializable:

username: Alexia

password: 123456

read after Serializable:

username: Alexia

password: null

密碼字段為null,説明反序列化時根本沒有從文件中獲取到信息。

  2. transient使用小結

1)一旦變量被transient修飾,變量將不再是對象持久化的一部分,該變量內容在序列化後無法獲得訪問。

2)transient關鍵字只能修飾變量,而不能修飾方法和類。注意,本地變量是不能被transient關鍵字修飾的。變量如果是用户自定義類變量,則該類需要實現Serializable接口。

3)被transient關鍵字修飾的變量不再能被序列化,一個靜態變量不管是否被transient修飾,均不能被序列化。

第三點可能有些人很迷惑,因為發現在User類中的username字段前加上static關鍵字後,程序運行結果依然不變,即static類型的username也讀出來為“Alexia”了,這不與第三點説的矛盾嗎?實際上是這樣的:第三點確實沒錯(一個靜態變量不管是否被transient修飾,均不能被序列化),反序列化後類中static型變量username的值為當前JVM中對應static變量的值,這個值是JVM中的不是反序列化得出的,不相信?好吧,下面我來證明

import InputStream;

import NotFoundException;

import OutputStream;

import ception;

import ctInputStream;

import ctOutputStream;

import alizable;

/**

* @description 使用transient關鍵字不序列化某個變量

* 注意讀取的時候,讀取數據的順序一定要和存放數據的順序保持一致

*

* @author Alexia

* @date 2013-10-15

*/

public class TransientTest {

public static void main(String[] args) {

User user = new User();

sername("Alexia");

asswd("123456");

tln("read before Serializable: ");

tln("username: " + sername());

tln("password: " + asswd());

try {

ObjectOutputStream os = new ObjectOutputStream(

new FileOutputStream("C:/"));

eObject(user); // 將User對象寫進文件

h();

e();

} catch (FileNotFoundException e) {

tStackTrace();

} catch (IOException e) {

tStackTrace();

}

try {

// 在反序列化之前改變username的值

name = "jmwang";

ObjectInputStream is = new ObjectInputStream(new FileInputStream(

"C:/"));

user = (User) Object(); // 從流中讀取User的數據

e();

tln("nread after Serializable: ");

tln("username: " + sername());

tln("password: " + asswd());

} catch (FileNotFoundException e) {

tStackTrace();

} catch (IOException e) {

tStackTrace();

} catch (ClassNotFoundException e) {

tStackTrace();

}

}

}

class User implements Serializable {

private static final long serialVersionUID = 8294180014912103005L;

public static String username;

private transient String passwd;

public String getUsername() {

return username;

}

public void setUsername(String username) {

name = username;

}

public String getPasswd() {

return passwd;

}

public void setPasswd(String passwd) {

wd = passwd;

}

}

  運行結果為:

read before Serializable:

username: Alexia

password: 123456

read after Serializable:

username: jmwang

password: null

這説明反序列化後類中static型變量username的值為當前JVM中對應static變量的值,為修改後jmwang,而不是序列化時的值Alexia。