As all the member variables of java LinkedList is transient, what will be the use of implementing Serializable?
Asked Answered
R

2

8

1) In LinkedList implementation all the 3 member varaibles size, first, last are transient, then what is the use of implementing serializable?

2) What will be persisted when we try to serialize a LinkedList?

Raymonraymond answered 19/9, 2014 at 3:41 Comment(0)
B
11

You'll notice LinkedList has writeObject and readObject methods. These write/read the size of the list and write/read each object in the list.

Note that these methods (but also the fields) are private so the implementation may change. Don't depend on it. Depend on the interfaces.

Bobsledding answered 19/9, 2014 at 3:44 Comment(1)
Otherwise serializing would be by default, which would be recursive, and for a large list would easily blow the stack.Martynne
W
3

Have a look at readObject() and writeObject(). It will serialize entries of the List not the list itself, and reconstructs it from the entries.

Wimer answered 19/9, 2014 at 3:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.