亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 1084 | 回復: 0
打印 上一主題 下一主題

java中的transient(轉(zhuǎn)載) [復制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2007-09-12 09:27 |只看該作者 |倒序瀏覽
Java中的transient
    改bug,發(fā)現(xiàn)一個保留字transient。很奇怪,從來沒見過,也從來沒用過。
    用google查了一把,大概意思是:Java語言的關(guān)鍵字,用來表示一個域不是該對象串行化的一部分。當一個對象被串行化的時候,transient型變量的值不包括在串行化的表示中,然而非transient型的變量是被包括進去的。還是不大明白。
    后來,終于搜到這篇文章,寫得很詳細。
Be Careful With Transient Data
    怕萬一這篇文章鏈接失效,收藏起來。
Be Careful With Transient Data
   
    終于明白了。
    當串行化某個對象時,如果該對象的某個變量是transient,那么這個變量不會被串行化進去。也就是說,假設(shè)某個類的成員變量是transient,那么當通過ObjectOutputStream把這個類的某個實例保存到磁盤上時,實際上transient變量的值是不會保存的。因為當從磁盤中讀出這個對象的時候,對象的該變量會沒有被賦值。
    另外這篇文章還提到,當從磁盤中讀出某個類的實例時,實際上并不會執(zhí)行這個類的構(gòu)造函數(shù),而是讀取這個類的實例的狀態(tài),并且把這個狀態(tài)付給這個類的對象。這點我以前似乎不知道。
Trackback:
http://tb.blog.csdn.net/TrackBack.aspx?PostId=1064847


Be Careful With Transient Data

(原文來自
http://www.devx.com/tips/Tip/13726
)
Expertise: Intermediate
Language: Java
January 28, 2000
Be Careful With Transient Data
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.
First, let's have some backgrounder code with Java's serialization.
Suppose we define a class as:

public class LoggingInfo implements java.io.Serializable
{
    private Date loggingDate = new Date();
    private String uid;
    private transient String pwd;
   
    LoggingInfo(String user, String password)
    {
        uid = user;
        pwd = password;
    }
    public String toString()
    {
        String password=null;
        if(pwd == null)
        {
        password = "NOT SET";
        }
        else
        {
            password = pwd;
        }
        return "logon info: \n   " + "user: " + uid +
            "\n   logging date : " + loggingDate.toString() +
            "\n   password: " + password;
    }
}
Now we can create an instance of this class and serialize it, and write the serialized object to disk as in:
LoggingInfo logInfo = new LoggingInfo("MIKE", "MECHANICS");
System.out.println(logInfo.toString());
try
{
   ObjectOutputStream o = new ObjectOutputStream(
                new FileOutputStream("logInfo.out"));
   o.writeObject(logInfo);
   o.close();
}
catch(Exception e) {//deal with exception}
To read the object back, we can write
try
{
   ObjectInputStream in =new ObjectInputStream(
                new FileInputStream("logInfo.out"));
   LoggingInfo logInfo = (LoggingInfo)in.readObject();
   System.out.println(logInfo.toString());
}
catch(Exception e) {//deal with exception}
If we run this code, we notice that the read-back object prints password as "NOT SET". This is exactly the effect we should have expected when we declared the pwd field as transient.
Now, let's see a potential problem that careless treatment of transient fields may cause. Suppose we modify our class definition and provide default values for the transient field, say we write:

public class GuestLoggingInfo implements java.io.Serializable
{
    private Date loggingDate = new Date();
    private String uid;
    private transient String pwd;
   
    GuestLoggingInfo()
    {
        uid = "guest";
        pwd = "guest";
    }
    public String toString()
    {
        //same as above
     }
}
Now, if we serialize an instance of GuestLoggingInfo, write it to disk, and read it back, we still see that the read-back object prints password as "NOT SET". In effect, the process of reading back (de-serializing) totally ignores the constructor of GuestLoggingInfo. So what happened?
The answer lies in the fact that the initialization code is not called because we are not initializing, in other words, we are not constructing a brand new object, but loading back the persistent state of an object of a class, and assigning that state to another object of the same class. Declaring the pwd field as transient, excludes the data for that field from the persistent state of our object. Then, upon de-serialization, since there is no data preserved for the pwd field, the field gets Java's default value for its type (null for String).
So, if you mark a field of an object as transient, and write that object to disk, expect to have the default value of the type of that field when you de-serialize the object, and not the actual value that the field had before its state was serialized. If a default value (or any meaningful value) is essential for a transient field of a de-serialized object, you have to assign it yourself either directly (if the field is public) or via a setter method.
Behrouz Fallahi
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1064817


本文來自ChinaUnix博客,如果查看原文請點:http://blog.chinaunix.net/u/22516/showart_380029.html
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP