雖然小編自己最熟的是Java,但很多Java基礎知識都不知道,比如transient關鍵字以前都沒用到過,所以不知道它的作用是什麼,今天做筆試題時發現有一題是關於這個的,於是花個時間整理下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。